0

I'm trying to figure out how to access in my js file the original page from the extension page. Lets say I'm in example.com/test.html, I want to know how do I access test.html from my extension (the download.js which is been called from download.html)

Edit: So I've added another js script in the middle however I never reach the download.js

Update Thanks to wOxxOm I've manage to get what I wanted, I've updated the question for follow searches.

Here is my code:

background.js

chrome.tabs.executeScript(null, { file: "jquery.min.js" }, function() {
    chrome.tabs.executeScript(null, { file: "download.js" }, function(results){

        JSON.stringify(results);

        var i;
        for (i = 0; i < results[0].length; ++i) {
            chrome.downloads.download({
                url: results[0][i],
                filename: "/Users/testusr/Documents/"
                });
        }

     });
 });

download.js

[].map.call(document.querySelectorAll('[data-bimg] img'), function(e) { 
return e.src; 
});

manifest.json

{
  "manifest_version": 2,
  "name": "test",
  "version": "1.0",
  "description": "This extension will download all product images",
  "browser_action": {
   "default_icon": "icon.png",
   "default_popup": "download.html"
  },
  "permissions": [
   "activeTab",
"downloads"
   ]
}

download.html

<!doctype html>
<html>
  <head>
    <title>Test</title>
    <script src="background.js"></script>
  </head>
  <body>
    <h1>Test</h1>
  </body>
</html>
Community
  • 1
  • 1
Nir
  • 2,017
  • 8
  • 35
  • 54
  • Use [`chrome.tabs.executeScript`](https://developer.chrome.com/extensions/tabs#method-executeScript) to inject script into the active tab. That is one of the [capabilities of the `"activeTab"` permission](https://developer.chrome.com/extensions/activeTab#what-activeTab-allows). – Ross Allen Feb 24 '17 at 23:54
  • 3
    Possible duplicate of [How to access the webpage DOM rather than the extension page DOM?](http://stackoverflow.com/questions/4532236/how-to-access-the-webpage-dom-rather-than-the-extension-page-dom) – wOxxOm Feb 25 '17 at 02:55
  • As I use `chrome.tabs.executeScript` in `background.js` I don't reach the `download.js` file – Nir Feb 25 '17 at 09:33
  • Have you actually used console.log or debugging with a breakpoint? You should have seen an error that says chrome.downloads API isn't available in a content script. So you'll have to send the image url from the injected script back into the background page which will download it. See the example I've linked - the results are reported in `console.log(results[0])` – wOxxOm Feb 25 '17 at 13:37
  • Yes of course I use (this how I was able to see I don't reach the download.js file). I didn't know that on the api, so I will move it to the outside js file as a return. But still I dont reach the download.js file.. – Nir Feb 25 '17 at 14:35
  • Let me guess: your executeScript is called from browserAction.onClicked? It won't work when `default_popup` is declared in manifest. – wOxxOm Feb 25 '17 at 17:55
  • No, without it. I tried to return a random string and I manage to get that string in the background.js however I never see the download.js in the debug window(therefor I don't know why my code return empty array with the original code). – Nir Feb 25 '17 at 18:03
  • Eh? So you use executeScript with null tabId parameter to inject into current page which is unpredictable when the extension starts its background page. – wOxxOm Feb 25 '17 at 18:07
  • Guessing isn't fun, actually. Upload a zip of your extension, I'll take a look, usually it's instantly obvious in the debugger. – wOxxOm Feb 25 '17 at 18:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136635/discussion-between-nir-and-woxxom). – Nir Feb 25 '17 at 18:28

0 Answers0