• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

chrome.tabs.executeScript不工作

用户头像
it1352
帮助1

问题说明

我正在学习使用chrome.tabs.executeScript命令。我用浏览器动作创建了一个简单的扩展。我的background.html文件目前如下所示:
$ b

I am trying to learn to use the chrome.tabs.executeScript commend. I've created a simple extension with a browser action. My background.html file currently looks like this:

<html>
<script>
    chrome.browserAction.onClicked.addListener(function(tab) {
        chrome.tabs.executeScript(null,{code:"document.body.bgColor='red'"});
        chrome.tabs.executeScript(null, {file: "content_script.js"});
    });
</script>
</html>

content_script.js文件包含document.body.bgColor ='red'。

The "content_script.js" file contains document.body.bgColor='red'.

当推动浏览器动作的按钮时,什么都没有发生。很显然,我错过了一些非常基本的东西。

When pushing the browser action's button nothing happens. Obviously I'm missing something very basic.

我已经用console.log检查过,当按下浏览器操作时,确实控件达到了chrome.tabs.executeScript调用。否则,我不知道如何检查我的内容脚本的代码是否运行(它似乎不是; console.log我放入内容脚本没有效果,但可能它不应该有一个,即使脚本运行成功)。

I've checked with console.log that indeed control reaches the chrome.tabs.executeScript calls when the browser action is pressed. Otherwise I'm not sure how to even check if my content script's code is run (it seems not; console.log I put in the content script has no effect, but maybe it shouldn't have one even if the script is run successfully).

正确答案

#1

确保您在清单中拥有域和标签权限:


$ b

Make sure you have domain and tab permissions in the manifest:

"permissions": [
    "tabs", "http://*/*", "https://*/*"
]

然后改变主体颜色尝试:

Then to change body color try:

chrome.tabs.executeScript(null,{code:"document.body.style.backgroundColor='red'"});

请注意,内容脚本不会被注入任何 chrome:/ / 或扩展程序库页面。

Also keep in mind that content scripts are not injected into any chrome:// or extension gallery pages.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfgijb
系列文章
更多 icon
同类精品
更多 icon
继续加载