1

I have been creating my first Chrome Extension, and it suppose to do something simple find a specific href and donwload it.

The extension is a Page Action so it is only triggered in the url page that I want, and a using the chrome.download on a specific href set is working.

Here my manifest.json:

    {
"name": "HrefDownloader",
"version": "1.0",
"manifest_version": 2,

"description":"Download all specific hrefs",
"background":{
    "scripts": ["background.js"],
    "persistent": false
},
"icons":
{
"128": "HrefDownloader.png"
},

"page_action":{
"default_title":"Href  downloader",
"default_popup": "popup.html"
},

"permissions":[
"downloads","storage","declarativeContent","<all_urls>"
]
}

Background.js:

chrome.runtime.onInstalled.addListener(function() {
       chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
      chrome.declarativeContent.onPageChanged.addRules([{
        conditions: [new chrome.declarativeContent.PageStateMatcher({
          pageUrl: {hostEquals: 'url.domain.com'},
        })
        ],
            actions: [new chrome.declarativeContent.ShowPageAction()]
      }]);
    });
  });

and my popup.js:

$(function(){
    $('#DownloadAll').click(function(){
        chrome.downloads.download({url: 'https://url.domain.com/something/Download/Document/10f7cb46-1dc7-56eb-8bde-9e3f55d6baa2'})
    })
})

This is working fine, but what I want is to download all the links that start with "https://url.domain.com/something/Download/Document/"

so I tried to get all the links and then download with a for loop:

$(document).ready(function() {
    var links = document.getElementsByTagName("a'href=https://url.domain.com/something/Download/Document/'")
});

$(function(){
    $('#DownloadAll').click(function(){
        for (var i = 0; i < links.length;i++){
            chrome.downloads.download({url: links[i]})
        }

    })
})

I got an that links is not defined but I have defined it so I am not sure what to do now.

For information I am completly new to JS and jQuery, I have been learning how to make a chrome extension since last thursday.

I do want a solution but I also wants to understand what is wrong and what should I check in order to make it work.

Thank you for reading!

Edit: Corrected i++ missing from the code

Smoogy
  • 45
  • 6
  • Could you give us the exact error? Also, your `for` loop is missing the `i++`. – Aioros May 18 '20 at 15:53
  • the error is: Uncaught ReferenceError: links is not defined, yes i++ was removed by mistake when copy pasting :) – Smoogy May 18 '20 at 15:56
  • Ok. You define `links` in a function, so that's its scope. You can't see it from the other functions. You might just want to declare it at the top of your popup. – Aioros May 18 '20 at 15:59
  • @Aioros thank you, I thought I needed to call the function to do this! The error message is now gone, but still no luck , nothing is happening. – Smoogy May 18 '20 at 16:03
  • `"a'href=https://url.domain.com/something/Download/Document/'"` is an invalid tag name. You probably wanted `document.querySelectorAll('a[href^="https://url.domain.com/......"]')` (see also tutorials on working with DOM in JavaScript) however it should be done in content script as explained in the [topic I've linked](/a/4532567). See also the official [demo extensions](https://developer.chrome.com/extensions/samples). – wOxxOm May 18 '20 at 16:57
  • @wOxxOm Thanks a lot for putting me on the right track, I was able to make the chrome extension work the way I wanted! – Smoogy May 19 '20 at 14:41

0 Answers0