134 lines
3.4 KiB
JavaScript
134 lines
3.4 KiB
JavaScript
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"]
|
|
);
|