commit 8b5936d465b113d4fcad33f49590cdc7ad4cca04 Author: flatgreen Date: Thu Aug 15 19:54:16 2019 +0200 Initial commit (before Mozilla) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cc786a9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +doc/ \ No newline at end of file diff --git a/background.js b/background.js new file mode 100644 index 0000000..c1afbbc --- /dev/null +++ b/background.js @@ -0,0 +1,133 @@ +var JD2_API_URL = 'http://localhost:9666/flashgot?'; +var JD2_REFERER = 'localhost'; +var JD2_MATCH_URL = "*://*/flashgot?*"; + + +function onError(error) { + console.log('UrlToJD2_error: ' + error); +} + +// On startup, check autostart option stored setting or default value +const gettingStoredSettings = browser.storage.local.get(); +gettingStoredSettings.then((item) => { + if (!item.autostart) { + browser.storage.local.set({ autostart: true }) + } +}, + onError); + + +function makeRequest(url) { + var req = new XMLHttpRequest(); + req.open('GET', url, true); + req.send(null); + req.onload = function () { + // console.log(req.status); + if (req.readyState === 4 && req.status === 200) { + // console.log(req.responseText); + // notify... + } else { + onError(req.statusText); + } + } +} + +function urlConstruct(url, autostart) { + return JD2_API_URL + "autostart=" + autostart + "&urls=" + url; +} + + +function bool_to_int(bool) { + return (bool == true) ? 1 : 0; +} + +// main +function loadTabUrlToJD2Crawl(storedSettings) { + var gettingActiveTab = browser.tabs.query({ active: true, currentWindow: true }); + gettingActiveTab.then((tabs) => { + if (tabs[0]) { + currentTabUrl = tabs[0].url; + url_for_JD2 = urlConstruct(currentTabUrl, bool_to_int(storedSettings.autostart)); + // console.log(url_for_JD2); + makeRequest(url_for_JD2); + } + }, onError); +} + + +browser.pageAction.onClicked.addListener(() => { + const gettingStoredSettings = browser.storage.local.get(); + gettingStoredSettings.then(loadTabUrlToJD2Crawl, onError); +}); + + +// menu +const sendToJD2Id = "send-to-JD2"; +function onCreated() { + if (browser.runtime.lastError) { + onError(browser.runtime.lastError); + } +} + +browser.menus.create({ + id: sendToJD2Id, + title: "send to JD2", + contexts: ["link"] +}, onCreated); + + +browser.menus.onClicked.addListener((info, tab) => { + if (info.menuItemId === sendToJD2Id) { + const gettingStoredSettings = browser.storage.local.get(); + gettingStoredSettings.then((storedSettings) => { + url_for_JD2 = urlConstruct(info.linkUrl, bool_to_int(storedSettings.autostart)); + makeRequest(url_for_JD2); + }, onError); + } +}); + +function updateMenuItem(link) { + browser.menus.update(sendToJD2Id, { + title: `Send to JD2: "${link}"` + }); + browser.menus.refresh(); +} + +browser.menus.onShown.addListener(info => { + if (!info.linkUrl) { + return; + } + updateMenuItem(info.linkUrl); +}); + + +// Some functions for a good referer ! + +// https://stackoverflow.com/a/11602753 +function mod_headers(header_array, p_name, p_value) { + var did_set = false; + for (var i in header_array) { + var header = header_array[i]; + var name = header.name; + // var value = header.value; + // If the header is already present, change it: + if (name == p_name) { + header.value = p_value; + did_set = true; + } + } + // if it is not, add it: + if (!did_set) { header_array.push({ name: p_name, value: p_value }); } +} + +function rewriteHeader(e) { + mod_headers(e.requestHeaders, 'Referer', JD2_REFERER); + // for (var header of e.requestHeaders) { console.log(header.name + '::' + header.value); } + return { requestHeaders: e.requestHeaders }; +} + +browser.webRequest.onBeforeSendHeaders.addListener( + rewriteHeader, + { urls: [JD2_MATCH_URL] }, + ["blocking", "requestHeaders"] +); diff --git a/icons/urltojd2-19.png b/icons/urltojd2-19.png new file mode 100644 index 0000000..401fd50 Binary files /dev/null and b/icons/urltojd2-19.png differ diff --git a/icons/urltojd2-38.png b/icons/urltojd2-38.png new file mode 100644 index 0000000..1c78d0b Binary files /dev/null and b/icons/urltojd2-38.png differ diff --git a/icons/urltojd2-48.png b/icons/urltojd2-48.png new file mode 100644 index 0000000..22249a5 Binary files /dev/null and b/icons/urltojd2-48.png differ diff --git a/icons/urltojd2-96.png b/icons/urltojd2-96.png new file mode 100644 index 0000000..4970eac Binary files /dev/null and b/icons/urltojd2-96.png differ diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..e289751 --- /dev/null +++ b/manifest.json @@ -0,0 +1,51 @@ +{ + "manifest_version": 2, + "name": "UrlToJD2", + "version": "1.0", + "description": "Push the current url navigator to JDownloader 2", + + "developer": { + "name": "GTeam", + "url": "https://framagit.org/GTeam/UrlToJD2" + }, + + "browser_specific_settings": { + "gecko": { + "id": "urltojd2@gteam.fr", + "strict_min_version": "55" + } + }, + + "icons": { + "48": "icons/urltojd2-48.png", + "96": "icons/urltojd2-96.png" + }, + + "background": { + "scripts": ["background.js"] + }, + + "page_action": { + "default_icon": { + "19": "icons/urltojd2-19.png", + "38": "icons/urltojd2-38.png" + }, + "show_matches": [""], + "default_title": "Send current url to JD2" + }, + + "permissions": [ + "activeTab", + "*://*/flashgot?*", + "webRequest", + "webRequestBlocking", + "storage", + "menus" + ], + + "options_ui": { + "page": "options/options.html", + "browser_style": true + } + +} \ No newline at end of file diff --git a/options/options.html b/options/options.html new file mode 100644 index 0000000..3fae68e --- /dev/null +++ b/options/options.html @@ -0,0 +1,19 @@ + + + + + + + + +
+

Autostart

+ + +
+ + + + + + \ No newline at end of file diff --git a/options/options.js b/options/options.js new file mode 100644 index 0000000..51846b7 --- /dev/null +++ b/options/options.js @@ -0,0 +1,29 @@ +function onError(error) { + console.log('UrlToJD2_options_error: ' + error); +} + +// autostart +var chck_autostart = document.getElementById("autostart"); + + +chck_autostart.onchange = function () { + if (chck_autostart.checked) { + browser.storage.local.set({ autostart: true }); + } else { + browser.storage.local.set({ autostart: false }); + } +} + + +// Update the options UI or default setting +function updateUI(restoredSettings) { + if (restoredSettings.autostart == undefined) { + chck_autostart.checked = true; //default + } else { + chck_autostart.checked = restoredSettings.autostart; + } +} + +// On opening the options page +const gettingStoredSettings = browser.storage.local.get(); +gettingStoredSettings.then(updateUI, onError); \ No newline at end of file