Hello, I just can not implement in the extension for chrome.
The xml http request is sent by the head method, then I cancel the redirect in the chrome.webRequest.onHeadersReceived event, chrome.webRequest.onHeadersReceived I catch the http status code in the chrome.webRequest.onHeadersReceived event.
And you need if it is equal to 302 or 301 (a redirect occurs), then it is canceled, and ask the user ( confirm: "внимание редирект" ) if he wants to download the file, if yes - then:
chrome.downloads.download({ url: details.url, }); if not then do nothing.
And if the http status code is 200, then just start downloading, without confirm :
chrome.downloads.download({ url: details.url, }); Here is what I have manifest.json for now :
{ "author": "me :D", "browser_action": { "default_icon": "images/Icon-128.png", "default_title": "Мегафон бесплатный траффик", "default_popup": "popup.html" }, "background": { "persistent": true, "scripts": ["background.js"] }, "content_scripts": [{ "js": ["content.js"], "matches": ["http://m.megafonpro.ru/*/proxy/*"], "run_at": "document_start" }], "description": "test extension", "options_page": "options.html", "icons": { "128": "images/Icon-128.png", "48": "images/Icon-48.png", "16": "images/Icon-16.png" }, "manifest_version": 2, "name": "Megafon Free Traffic", "permissions": ["<all_urls>", "webRequest", "webRequestBlocking", "tabs", "activeTab", "", "downloads", "contextMenus"], "short_name": "MegaFon", "version": "1", } content.js :
var clickedEl = null; document.addEventListener("mousedown", function(event) { if (event.button == 2) { clickedEl = event.target; } }, true); chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { if (request == "getHref") { sendResponse({ href: clickedEl.href }); } }); Here the user clicks the right mouse button on the link and an item appears from the context menu "download", and href is sent to background.js :
chrome.tabs.onUpdated.addListener(function(id, info, tab) { if (info && info.status && (info.status.toLowerCase() === 'complete')) { if (/m.megafonpro.ru/.test(tab.url)) { chrome.webRequest.onBeforeRequest.addListener( function(details) { if (details.url.indexOf("http://m.megafonpro.ru/") == -1) { return { cancel: (details.url.indexOf("http://m.megafonpro.ru/mailru/proxy/") == -1 && details.url.indexOf("http://m.megafonpro.ru/twitter/leave") == -1 && details.url.indexOf("http://m.megafonpro.ru/mailru/static/") == -1 && details.url.indexOf("http://m.megafonpro.ru/login") == -1 && details.url.indexOf("chrome-extension://") == -1 && details.url.indexOf("http://web.archive.org/web/20140306220001/http://m.megafonpro.ru/home?from_404=1") == -1) } if (cancel) { return { redirectUrl: void 0 }; } } }, { urls: ["<all_urls>"] }, ["blocking"]); } } chrome.webRequest.onHeadersReceived.addListener(function(details) { preventDownload = false; if (details.method == "HEAD") { if (details.statusCode == "302" && details.statusCode == "301") { alert('ahah'); if (confirm("Файл не удаётся загрузить,попробовать скачать по траффику ?")) { preventDownload = true; } } if (details.statusCode == "200") { preventDownload = true; } if (preventDownload) { chrome.downloads.download({ url: details.url, }); } } }, { urls: ["http://*/*"] }, ["responseHeaders"]); }); function mycallback(info, tab, getdownload) { chrome.tabs.sendRequest(tab.id, "getHref", function(clickedEl) { var xhr = new XMLHttpRequest(); xhr.open("HEAD", clickedEl.href.replace('proxy', 'static'), true); xhr.send(null); }); } var contexts = ["link"]; for (var i = 0; i < contexts.length; i++) { var context = contexts[i]; var id = chrome.contextMenus.create({ title: "скачать", contexts: [context], onclick: mycallback }); } Next in the background, an xml http request is sent — a head request with a slightly changed address.
What specifically does not work:
- Downloading starts only if http status code == 200, otherwise it is simply ignored,
confirmis not displayed either. - The
onHeadersReceivedeventonHeadersReceivedtriggered 4 times, so it downloads 4 times. - For some reason, all requests are blocked in all tabs, that is, it is impossible to enter any site except m.megafonpro.ru, it says that the extension has blocked this site.
- And in general, it seems to me that it’s a shit, a lot of excess, can you tweak how it will be better?