0

In order for my extension to work correctly I am injecting my Javascript into the page itself via my extension.

function injectScript(file, node) {
    var th = document.getElementsByTagName(node)[0];
    var s = document.createElement('script');
    s.setAttribute('type', 'text/javascript');
    s.setAttribute('src', file);
    th.appendChild(s);
}

injectScript( chrome.extension.getURL('script.js'), 'body');

The problem with this approach is I am doing all of my manipulations in the injected script, rather than the main js file. This means I don't have access to the Chrome API. I want to use chrome.storage however I can't now.

Is there a way to get the data from the variable on the website and pass it back to my extension without having to work in the page itself like this?

Edit, attempting to use window.variable (in chrome console):

In top frame:

window.delimitedMeetingInfo
["1|FOOD*2010*DE|2015/01/05|2015/04/17|LEC|Days TBA|Times TBA|Room TBA", "2|CIS*2500*0104|2015/01/05|2015/04/17|LEC|Mon, Wed, Fri |02:30PM - 03:20PM|ROZH, Room 101", "2|CIS*2500*0104|2015/01/05|2015/04/17|LAB|Wed |10:30AM - 12:20PM|THRN, Room 2418", "3|CIS*2170*0101|2015/01/05|2015/04/17|LEC|Tues, Thur |08:30AM - 09:50AM|CRSC, Room 116", "3|CIS*2170*0101|2015/01/05|2015/04/17|LAB|Tues |02:30PM - 04:20PM|MCKN, Room 233", "4|CIS*1910*0105|2015/01/05|2015/04/17|LEC|Tues, Thur |11:30AM - 12:50PM|MACN, Room 105", "4|CIS*1910*0105|2015/01/05|2015/04/17|LAB|Fri |12:30PM - 02:20PM|MCKN, Room 229", "5|CIS*2250*0103|2015/01/05|2015/04/17|LEC|Mon, Wed, Fri |09:30AM - 10:20AM|JTP, Room 214", "5|CIS*2250*0103|2015/01/05|2015/04/17|LAB|Wed |12:30PM - 02:20PM|REYN, Room 114"]

In extension frame:

window.delimitedMeetingInfo
undefined
ComputerLocus
  • 3,448
  • 10
  • 47
  • 96
  • Pure spitballing here, but would `window.variable` not work? The only time I've worked with extensions they were self-contained, so I've not had to deal with that particular quandary. – Reinstate Monica Cellio Nov 25 '14 at 15:43
  • @Archer it sucks that I need to use the variable in the page, but in order to ensure my information is accurate I need to use it. Check the edit I will make that shows what you suggest not working. – ComputerLocus Nov 25 '14 at 15:48
  • The example you've shown is two different variables. If you always prefix with `window.` then it should work. – Reinstate Monica Cellio Nov 25 '14 at 15:50
  • @Archer If I do `window.delimitedMeetingInfo` on the pages frame in Chrome I get the results, if I do `window.delimitedMeetingInfo` in the extensions frame I get `undefined` – ComputerLocus Nov 25 '14 at 15:51
  • Not sure what you mean by "extension frame". Whenever I've done anything with extensions they've all run in the same window. Do you literally mean you're running the script in another frame (as in an iframe)? – Reinstate Monica Cellio Nov 25 '14 at 16:41
  • 1
    Possibly a duplicate of [Can a site invoke a browser extension?](http://stackoverflow.com/questions/10526995/can-a-site-invoke-a-browser-extension), but that question/answer might be exactly what you're trying to avoid? I'm not sure. – apsillers Nov 25 '14 at 16:57
  • @Archer in the Google Chrome console you can choose if you are within the scope of the "top frame" which is the page itself, or the extensions. If I switch to the extension I don't have access to the pages variables. – ComputerLocus Nov 25 '14 at 17:02
  • @apsillers I am not sure if this is what I want. I basically want my code contained within the extension, and something to allow me to get the variable from the page itself, but without having to run ALL the code within it. – ComputerLocus Nov 25 '14 at 17:03
  • Okay, I've opened up one of my extensions and had a play around to give myself some context, and I can see what the problem is now, but I don't see a clean solution. The only thing I can think of is to create a DOM element in the injected script and set attributes (preferably data attributes) on that element. That can then be accessed by the extension as it's part of the DOM. My guess is that allowing an extension to manipulate script via variables would be a massive security issue, but that's just more spitballing. – Reinstate Monica Cellio Nov 25 '14 at 17:18
  • @Archer okay, I may need to do what apsillers linked in his comment. Yes, I know chrome is blocking this action for security reasons, and I am trying to find a solution to get around this issue. – ComputerLocus Nov 25 '14 at 17:27

0 Answers0