如何在Chrome扩展的后台页面上执行脚本 [英] How To Execute Scripts On Background Page Of A Chrome Extension
问题描述
我正在开发Chrome扩展程序。我的manifest.json是:
page_action:{
default_icon:{
19 :icons / icon19.png,
38:icons / icon38.jpg
},
default_popup:options.html
},
在我的扩展中,我尝试执行试图在实际网页上运行/ / p>
//在options.html
chrome.tabs.executescript(null,{code:alert('hello! ');});
但Chrome扩展调试器输出此错误:
Uncaught TypeError:Object#< Object>没有方法'performcript'
我想反映对content_script或实际选项的任何更改
在page_action范围内,我如何到达chrome对象?
现在尝试更改执行脚本
到 executeScript
大写'S'是重要的。
executeScript
和 insertCSS
主要用于将js或css文件注入给定的选项卡而不是原始代码。当你想在非常特定的情况下注入代码时,会使用它们。在你的代码中,你并没有指定一个 id
作为注入代码的标签,所以它只是在给定的上下文中注入它。如果你在消息传递时使用了 chrome.tabs.query
,你应该能够将代码注入到实际的标签中。
当您从 我不知道您正在尝试进行哪些更改的具体细节。如果它们纯粹是 I am developing a Chrome extention. My manifest.json is: In my extension I am trying to execute scripts which attempt to run/reach on actual web page: But Chrome extension debugger outputs this error: I want to reflect any changes on options to content_script or actual web page immidiately. In page_action scope, how can I reach "chrome" object? For now try changing It seems that you are trying to affect some sort of change on a given tab when you change options in a popup from a I don't know the exact details of what kinds of changes you are trying to make. If they are purely 这篇关于如何在Chrome扩展的后台页面上执行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!页面操作>我个人会推荐更改弹出窗口中的选项时,您似乎试图影响给定选项卡上的某种更改使用
jQuery
进行实际更改,同时使用从另一个问题传递的消息向给定选项卡发送消息。您可以尝试使用 css
或 Javascript
进行编程注入,但是选项中的连续更改可能会产生不良结果。
css
,那么您可以将一个样式表注入到页面中,并且只需在要更改的事物上切换一个类,并为每个方案创建一个类。如果你更详细地描述你想要做的事情,我可以给出更详细的建议。"page_action": {
"default_icon": {
"19": "icons/icon19.png",
"38": "icons/icon38.jpg"
},
"default_popup": "options.html"
},
// in options.html
chrome.tabs.executescript(null,{code:"alert('hello!');"}) ;
Uncaught TypeError: Object #<Object> has no method 'executescript'
executescript
to executeScript
the capital 'S' is important.executeScript
and insertCSS
are mostly used to inject a js or css file into a given tab rather than the raw code. They are used when you want to inject that code in very specific situations. In your code you don't specify an id
for what tab to inject the code into, so it just injects it in the given context. If you use a chrome.tabs.query
like with the message passing, you should be able to inject the code into the actual tab.Page Action
I personally would recommend using jQuery
to make the actual changes while using the message passing from the other question to send a message to the given tab. You could try doing it with programmatic injection of css
or Javascript
, but successive changes in the options might have undesirable results. css
then you could have one stylesheet injected into the page and just toggle a class on the things you want to change and have a class for each scenario. If you describe what you are trying to do in more detail I could give a more detailed suggestion.