【问题标题】:To print console.log message in active window console instead of background..在活动窗口控制台而不是后台打印 console.log 消息。
【发布时间】:2016-09-25 05:01:36
【问题描述】:

我开始构建 chrome 扩展。最初,我从一个简单的扩展开始,它在后台控制台中打印 hello。如何在活动窗口控制台中打印它。

这是我的 manifest.json 文件

 {
     "manifest_version": 2,
     "name": "example",
     "version": "0.1",
     "description": "My Chrome Extension",
  "icons":  { 

    },

  "background": {
    "scripts": ["js/background.js"] 
  },


  "browser_action": {
    "default_title": "My test Environment"

    },

"permissions": [
    "background",
    "storage",
    "tabs", 
    "http://*/*",
    "https://*/*"
    ] 
  }

这是我的 background.js 文件的内容 一个简单的
console.log("你好");

【问题讨论】:

标签: google-chrome-extension


【解决方案1】:

查看Content Script,您可以使用manifest.json injectionProgramming injection 来确保您的代码在当前网页的上下文中运行。

  • 前者需要Message PassingStorage来保证内容脚本与后台页面的通信;
  • 对于后者,在background.js中试试下面的代码 chrome.tabs.executeScript({code: "console.log('hello');"});

【讨论】:

    猜你喜欢
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多