Code Is Executing Multiple Times Per Event: Multiple Downloads
I want to download a JSON object from within the content script. At first, when I request the download, it downloads one file, but at the second request, it downloads two files; at
Solution 1:
As is usually the case with this pattern of problem, the issue is that you are adding multiple anonymous listeners to an event. Specifically, you are adding yet another chrome.runtime.onMessage
listener each time the action_button
is clicked. You need to add the listener only once.
The simple solution to this is to just add the chrome.runtime.onMessage
once:
chrome.browserAction.onClicked.addListener(function (tab) {
download();
});
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
//Alert the messageconsole.log(request);
chrome.downloads.download({
url: request.method,
filename: request.name
}, function (downloadId) {
});
//You have to choose which part of the response you want to display // ie. request.method//alert('The message from the content script: ' + request.method);//Construct & send a responsesendResponse({
response: "Message received"
});
});
functiondownload() {
chrome.tabs.executeScript(null, {
file: "jquery.js"
}, function () {
chrome.tabs.executeScript(null, {
file: "content_script.js"
});
});
}
Post a Comment for "Code Is Executing Multiple Times Per Event: Multiple Downloads"