chrome.tabs.executeScript不工作? [英] chrome.tabs.executeScript not working?

查看:225
本文介绍了chrome.tabs.executeScript不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习使用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).

推荐答案

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


$ 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.

这篇关于chrome.tabs.executeScript不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆