最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to access page variables from Chrome extension background script - Stack Overflow

programmeradmin3浏览0评论

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

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 municating 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"]
  },

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

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 municating 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"]
  },
Share Improve this question edited May 23, 2017 at 12:09 CommunityBot 11 silver badge asked Sep 9, 2013 at 21:30 Matt ZeunertMatt Zeunert 16.6k7 gold badges54 silver badges83 bronze badges 2
  • Please expand your question showing your manifest file and the rest of your code. How do you know that "the script was inserted correctly"? – rsanchez Commented Sep 10, 2013 at 11:40
  • @rsanchez Have added the relevant section of the manifest. By "was inserted correctly" I mean that the DOM node was inserted as I would expect, and the log output is printed periodically. The problem is that the value of window.testVar is undefined if I run inject.js from a background script. – Matt Zeunert Commented Sep 10, 2013 at 20:34
Add a ment  | 

3 Answers 3

Reset to default 5

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.

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

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

Your inject.js should 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.

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.

Consider the following:

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

发布评论

评论列表(0)

  1. 暂无评论