init
This commit is contained in:
commit
b64a617615
14 files changed
+705
No files matched your search
+103
@@ -0,0 +1,103 @@
|
||||
function set_cookies(cookies) {
|
||||
for (let cookie of cookies) {
|
||||
browser.cookies.set(cookie)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function set_proxy(PROXY) {
|
||||
// PROXY = {
|
||||
// "type": "http",
|
||||
// "host": "IP address",
|
||||
// "port": "PORT",
|
||||
// "username": "USER NAME",
|
||||
// "password": "PASSWORD"
|
||||
// }
|
||||
const TARGET = "<all_urls>";
|
||||
const CREDENTIALS = {
|
||||
username: PROXY.username,
|
||||
password: PROXY.password
|
||||
}
|
||||
const pendingRequests = [];
|
||||
|
||||
function handleProxyRequest(requestInfo) {
|
||||
return {type: PROXY.type, host: PROXY.host, port: PROXY.port};
|
||||
}
|
||||
|
||||
// A request has completed.
|
||||
// We can stop worrying about it.
|
||||
function completed(requestDetails) {
|
||||
var index = pendingRequests.indexOf(requestDetails.requestId);
|
||||
if (index > -1) {
|
||||
pendingRequests.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
function provideCredentialsSync(requestDetails) {
|
||||
// If we have seen this request before, then
|
||||
// assume our credentials were bad, and give up.
|
||||
if (pendingRequests.indexOf(requestDetails.requestId) != -1) {
|
||||
console.log(`bad credentials for: ${requestDetails.requestId}`);
|
||||
return {cancel: true};
|
||||
}
|
||||
pendingRequests.push(requestDetails.requestId);
|
||||
console.log(`providing credentials for: ${requestDetails.requestId}`);
|
||||
return {authCredentials: CREDENTIALS};
|
||||
}
|
||||
|
||||
browser.proxy.onRequest.addListener(
|
||||
handleProxyRequest,
|
||||
{urls: [TARGET]}
|
||||
);
|
||||
|
||||
browser.webRequest.onAuthRequired.addListener(
|
||||
provideCredentialsSync,
|
||||
{urls: [TARGET]},
|
||||
["blocking"]
|
||||
);
|
||||
|
||||
browser.webRequest.onCompleted.addListener(
|
||||
completed,
|
||||
{urls: [TARGET]}
|
||||
);
|
||||
|
||||
browser.webRequest.onErrorOccurred.addListener(
|
||||
completed,
|
||||
{urls: [TARGET]}
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
function main() {
|
||||
|
||||
function setProxy(result) {
|
||||
if (result.proxy) {
|
||||
console.log("Setup proxy...");
|
||||
set_proxy(result.proxy);
|
||||
}
|
||||
}
|
||||
|
||||
function setCookies(result) {
|
||||
if (result.cookies) {
|
||||
console.log("Setup cookies...");
|
||||
set_cookies(result.cookies);
|
||||
}
|
||||
}
|
||||
|
||||
function onError(error) {
|
||||
console.log(`Error: ${error}`);
|
||||
}
|
||||
|
||||
let proxy = browser.storage.sync.get("proxy");
|
||||
proxy.then(setProxy, onError);
|
||||
|
||||
let cookies = browser.storage.sync.get("cookies");
|
||||
cookies.then(setCookies, onError);
|
||||
}
|
||||
|
||||
|
||||
browser.runtime.onMessage.addListener(function(message, sender, reply) {
|
||||
if (message.update) {
|
||||
main();
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
</head>
|
||||
<body>
|
||||
<form id="options_form" method="POST">
|
||||
<input type="text" name="type" id="type">
|
||||
<input type="text" name="host" id="host">
|
||||
<input type="text" name="port" id="port">
|
||||
<input type="text" name="username" id="username">
|
||||
<input type="text" name="password" id="password">
|
||||
<input id="submit" type="submit" value="Submit">
|
||||
<textarea id="cookies" name="cookies"></textarea>
|
||||
</form>
|
||||
<script src="options.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,58 @@
|
||||
function saveOptions(e) {
|
||||
e.preventDefault();
|
||||
let cookies = [];
|
||||
try {
|
||||
cookies = JSON.parse(document.getElementById("cookies").value);
|
||||
} catch {};
|
||||
console.log(cookies)
|
||||
browser.storage.sync.set({
|
||||
proxy: {
|
||||
"type": document.getElementById("type").value,
|
||||
"host": document.getElementById("host").value,
|
||||
"port": document.getElementById("port").value,
|
||||
"username": document.getElementById("username").value,
|
||||
"password": document.getElementById("password").value
|
||||
},
|
||||
cookies: cookies
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function restoreOptions() {
|
||||
|
||||
function setCurrentProxy(result) {
|
||||
if (result.proxy) {
|
||||
document.getElementById("type").value = result.proxy.type;
|
||||
document.getElementById("host").value = result.proxy.host;
|
||||
document.getElementById("port").value = result.proxy.port;
|
||||
document.getElementById("username").value = result.proxy.username;
|
||||
document.getElementById("password").value = result.proxy.password;
|
||||
}
|
||||
}
|
||||
|
||||
function setCurrentCookies(result) {
|
||||
if (result.cookies) {
|
||||
document.getElementById("cookies").value = JSON.stringify(result.cookies);
|
||||
}
|
||||
}
|
||||
|
||||
function onError(error) {
|
||||
console.log(`Error: ${error}`);
|
||||
}
|
||||
|
||||
let proxy = browser.storage.sync.get("proxy");
|
||||
proxy.then(setCurrentProxy, onError);
|
||||
|
||||
let cookies = browser.storage.sync.get("cookies");
|
||||
cookies.then(setCurrentCookies, onError);
|
||||
}
|
||||
|
||||
|
||||
function notifyExtension(e) {
|
||||
browser.runtime.sendMessage({"update": true});
|
||||
}
|
||||
|
||||
|
||||
document.addEventListener("DOMContentLoaded", restoreOptions);
|
||||
document.getElementById("options_form").addEventListener("submit", saveOptions);
|
||||
window.addEventListener("submit", notifyExtension);
|
||||
Reference in new issue
Block a user