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>
10
  • Use chrome.tabs.executeScript to inject script into the active tab. That is one of the capabilities of the "activeTab" permission.
    – Ross Allen
    Feb 24, 2017 at 23:54
  • 3
    Possible duplicate of How to access the webpage DOM rather than the extension page DOM?
    – wOxxOm
    Feb 25, 2017 at 2:55
  • As I use chrome.tabs.executeScript in background.js I don't reach the download.js file
    – Nir
    Feb 25, 2017 at 9: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, 2017 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, 2017 at 14:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.