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

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

问题描述

我正在尝试学习使用 chrome.tabs.executeScript 指令.我创建了一个带有浏览器操作的简单扩展.我的 background.html 文件目前看起来像这样:

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

推荐答案

确保您在清单中拥有域和选项卡权限:

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天全站免登陆