0

I have a website which have an object stores in javascript variable, when I run images from web console this is the output:

Object {123: Array[3], 444: Array[3], 654: Array[3]}

I need to get this variable to my extension.

I tried using this code without success:

content.js:

function exeecuteCode(tab, code, callback) {
    chrome.tabs.executeScript(tab.id, {code}, response => {
        debugger;
    });
}

index.js:

(function() {
    document.addEventListener('DOMContentLoaded', () => {
        chrome.tabs.getSelected(null, tab => {
            executeCode(tab, "if (typeof images !== 'undefined') document.getElementsByTagName('body')[0].setAttribute('tmp_images', JSON.stringify(images));\n";);
        });
    }, false);
}());

My manifest.json includes the content.js file as follow.

manifest.json:

"content_scripts": [
    {
        "matches": ["<all_urls>"],
        "js": ["content.js"]
    }
]

I know how to retrieve back after i add the attribute to the body element.

The only thing that dowsn't work is the code above. I don't get any errors, and if I run the code in the tab console it works.

Thanks in advance for your help!

Ziki
  • 1,390
  • 1
  • 13
  • 34

1 Answers1

1
  1. chrome.tabs.executeScript can't be called in content scripts, you would need to move it to background page
  2. By default, content scripts would run at document_idle, which means DOMContentLoaded has been triggered before you inject the scripts. You would add runAt: 'document_start' when calling chrome.tabs.executeScript or just remove the outer DOMContentLoaded handlers, which depends on your needs.
Haibara Ai
  • 10,703
  • 2
  • 31
  • 47
  • this function: `function parseTabDOM(tab, callback) { chrome.tabs.executeScript(tab.id, {code: "document.all[0].outerHTML;"}, response => { callback(null, $.parseHTML(response[0])); }); }` also inside `content.js` and I use it the same as described in my question and it works.. – Ziki Jul 25 '16 at 10:42
  • @Ziki, see https://developer.chrome.com/extensions/content_scripts which api content scripts can use, and ensure you didn't also include content.js as a background script. – Haibara Ai Jul 25 '16 at 10:46
  • @Ziki This is simply impossible, which means it isn't the only place you're using `content.js` (which is usually a _bad_ idea). – Xan Jul 25 '16 at 13:55
  • Haibara and @Xan, I'll check and fix it. But as far as I understand from the guide you sent me, it's seems that I can't get the web page javascript, because it's isolated? Am I wrong? If it's possible, can you please provide me an example? – Ziki Jul 25 '16 at 15:33
  • Documentation on isolation: https://developer.chrome.com/extensions/content_scripts#execution-environment | Example of bypass: https://stackoverflow.com/questions/9515704/building-a-chrome-extension-inject-code-in-a-page-using-a-content-script – Xan Jul 25 '16 at 15:35
  • @Xan, from your google link: `They have access to the DOM of the page they are injected into, but not to any JavaScript variables or functions created by the page.` - I understand it's impossible. – Ziki Jul 25 '16 at 15:39
  • You seem to lack patience to read my second link, clearly labeled "example of **bypass**". It's a canonical source for that technique. – Xan Jul 25 '16 at 15:39