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.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfgijb
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20