【问题标题】:how to make change to web page using chrome.tabs.executeScript()如何使用 chrome.tabs.executeScript() 更改网页
【发布时间】:2019-06-22 16:06:39
【问题描述】:

我想在浏览器中单击我的扩展程序图标,并在我当前在浏览器中选择的网页/选项卡的控制台中显示“测试”。

manifest.json:

{
    "name": "some name",
    "version": "1.0",
    "description": "some description",
    "manifest_version": 2,
    "permissions": ["storage", "tabs", "activeTab"],


    "browser_action": {
      "default_title": "hello!",
      "default_popup": "popup.html",
      "default_icon": "icon.png"
    },

    "background": {
      "scripts": ["background.js"],
      "persistent": true
    }

}

background.js:

chrome.browserAction.onClicked.addListener(function(){

chrome.tabs.executeScript(null, {
    code: "test"
})

我已使用“选项卡”和“活动标签”来获取权限,因为我相信这些是运行此代码所必需的。

如果我单击扩展程序,它不会显示错误,但它也不会在我所在网页的控制台日志中显示“测试”。

从我提供的代码来看,这有什么明显的原因吗?

【问题讨论】:

  • "console.log('test')"替换"test"
  • 我偶然发现了这个问题所在:stackoverflow.com/questions/18766886/…
  • 当有 popup.html 时它可能不允许 onClicked 事件,因为该事件可以放在 popup.js 中。

标签: google-chrome-extension browser-action


【解决方案1】:
chrome.browserAction.onClicked.addListener(function(){
  browser.tabs.executeScript({
    code: `console.log('test');`
  });
});

来源:https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/executeScript

【讨论】:

  • @Steve 你试过了吗?
猜你喜欢
  • 1970-01-01
  • 2020-01-31
  • 1970-01-01
  • 1970-01-01
  • 2019-11-23
  • 2014-01-19
  • 1970-01-01
  • 1970-01-01
  • 2019-04-19
相关资源
最近更新 更多