2

Is there a simple way where I can access a global javascript variable through content-scripts in chrome extensions?

Accessing global object from content script in chrome extension

I followed the steps mentioned in the above link, but it did not work out for me. Any help would be much appreciated.

Thanks, Shankar

Community
  • 1
  • 1
Shankar
  • 89
  • 1
  • 2
  • 11
  • 1
    Where is the global variable declared? Is it in the webpage that you are loading? – Jophin Joseph May 14 '12 at 06:30
  • The method in the linked question (more specifically, [this one](http://stackoverflow.com/questions/9602022/chrome-extension-retrieving-gmails-original-message/9636008#9636008) works as expected. The working code, based on the linked Q&A is pasted here: http://pastebin.com/BcgRN02U. Have you declared `"manifest_version": 2`? If yes, see [this answer](http://stackoverflow.com/questions/10527625/google-chrome-extension-script-injections/10529675#10529675) for more information. – Rob W May 14 '12 at 12:53
  • @JophinJoseph No it is declared in a extension script file. I wanted to access it directly in content-script – Shankar May 14 '12 at 17:15
  • @RobW Ya I am using "manifest_version":2. Will try this and update how it went. – Shankar May 14 '12 at 17:15
  • possible duplicate of [Accessing global object from content script in chrome extension](http://stackoverflow.com/questions/10052259/accessing-global-object-from-content-script-in-chrome-extension) – NVI Aug 05 '14 at 18:17

2 Answers2

0

I managed to complete it. Thanks for the help. I used simple message passing to retrieve the value from the extension script to the content script. The place where I had missed was, the listener at the extension script needs to be at the background page (I think so). Once I changed that, it worked.

Preview
  • 35,317
  • 10
  • 92
  • 112
Shankar
  • 89
  • 1
  • 2
  • 11
  • 1
    can you give me the code for this. I want to store some data in global variable in my chrome extension – Asad Rao Mar 21 '19 at 11:56
0

For those from the future looking for an answer to this question, here's how I do it:

function getVariable(v) {
    var c = document.createElement("div");
    c.id = 'var-data';
    c.style.display = 'none';
    document.body.appendChild(c);
    var s = document.createElement('script');
    s.innerHTML = 'document.getElementById("var-data").innerText=JSON.stringify('+v+');';
    document.head.appendChild(s);
    var data = JSON.parse(c.innerText);
    c.remove();
    s.remove();
    return data;
}

And basic usage:

getVariable('globalVarIWantToAccess');

All this script goes in the content-script, not the code for the main webpage, which means that no co-operation is needed from the webpage itself. Basically, the getVariable function creates a script element which is injected into the main page. This script tag retrieves the requested global variable and puts the data into a new div. The function then gets this data from the new div, deletes the new div, deletes the new script element and returns the data.

Tiernan Crotty
  • 187
  • 1
  • 10
  • Can you explain how this works a little more? I'm guessing the point is to insert the data you want into the page that both script documents can access, but doesn't the getVariable() function delete it within the function anyway? Meaning the data you want doesn't exist when you call the function? I don't understand how getVariable could be called from a second .js document. – Zach Morris Aug 10 '22 at 22:11