javascript 如何从 Chrome 扩展后台脚本访问页面变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18707341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:56:40  来源:igfitidea点击:

How to access page variables from Chrome extension background script

javascriptgoogle-chrome-extensioncontent-script

提问by Matt Zeunert

With a content script you can inject a script tag into the DOM in order to access variables in the original page (as explained in this question).

使用内容脚本,您可以将脚本标记注入 DOM 以访问原始页面中的变量(如本问题所述)。

I want to avoid injecting my code into every page and instead only do that when the user clicks on the extension icon.

我想避免将我的代码注入每个页面,而是仅在用户单击扩展图标时才这样做。

When I tried using the same code as for the content script the values were undefined, although the script was inserted correctly.

当我尝试使用与内容脚本相同的代码时,尽管脚本已正确插入,但值未定义。

Is this possible? Otherwise is using a content script and communicating with it the preferred solution?

这可能吗?否则使用内容脚本并与之通信是首选解决方案吗?

Here is the code I'm using:

这是我正在使用的代码:

var scr = document.createElement("script");
scr.type="text/javascript";
scr.innerHTML = "setInterval('console.log(window.testVar)', 1000)"
document.body.appendChild(scr)


Manifest excerpt:

清单摘录:

 "permissions": [
    "tabs",
    "http://*/*", "https://*/*"
  ],
  "background": {
    "scripts": ["inject.js"]
  },

采纳答案by rsanchez

You can create a new background script with the following code:

您可以使用以下代码创建新的后台脚本:

chrome.browserAction.onClicked.addListener( function() {
  chrome.tabs.executeScript( { file: 'inject.js' } );
});

Your inject.jsshould stay as part of the extension, but you don't need to mention it in the manifest. This way, it will be run as a content script each time you press the extension icon. You didn't include the part of the manifest where you define the browserAction, but you just need to not specify a default_popup.

inject.js应该保留作为扩展的一部分,但您不需要在清单中提及它。这样,每次按下扩展图标时,它都会作为内容脚本运行。您没有包含定义 browserAction 的清单部分,但您只需要不指定default_popup.

回答by Krasimir

Nope. That's not possible. You may inject a script, but it only have an access to DOM and it could make DOM manipulations. It can not read javascript variables or execute functions in the context of the current page. Your javascript code is run in a sandbox and you are restricted only to the DOM elements.

不。那是不可能的。您可以注入脚本,但它只能访问 DOM,并且可以进行 DOM 操作。它无法在当前页面的上下文中读取 javascript 变量或执行函数。您的 javascript 代码在沙箱中运行,您只能使用 DOM 元素。

回答by Ryan Kimber

You can utilize a background script to execute JavaScript within the context of the page, however, your JavaScript is executed in an isolated environment from the JavaScript that is loaded from within the page.

您可以利用后台脚本在页面上下文中执行 JavaScript,但是,您的 JavaScript 是在与从页面内加载的 JavaScript 隔离的环境中执行的。

Consider the following:

考虑以下:

In page.html:

在 page.html 中:

<button id="myButton" onclick="console.log('Message from within the page');" />

In background script:

在后台脚本中:

chrome.tabs.executeScript({
  code: '$("#myButton").click(function() { console.log("Message from background script."); });'
});

Now, if you click the button within the page, you will find both log messages in the console window. The JavaScript contexts are isolated from each other, with the notable exceptions of messaging via postMessage and through the shared DOM.

现在,如果您单击页面中的按钮,您将在控制台窗口中找到两条日志消息。JavaScript 上下文彼此隔离,通过 postMessage 和共享 DOM 进行消息传递的显着例外。