Fixing cookies #319 #323

This commit is contained in:
ManeraKai 2022-06-11 09:57:41 +03:00
parent fd844da5e0
commit d81a4b9766
No known key found for this signature in database
GPG Key ID: 5ABC31FFD562E337
8 changed files with 67 additions and 75 deletions

View File

@ -44,6 +44,7 @@ async function initDefaults() {
"maps",
],
autoRedirect: false,
firstPartyIsolate: false,
}, () => resolve())
)
}

View File

@ -46,6 +46,9 @@ init();
browser.storage.onChanged.addListener(init)
function redirect(url, initiator) {
if (disableMaps) return;
if (initiator && initiator.host === "earth.google.com") return;
if (!url.href.match(targets)) return;
const mapCentreRegex = /@(-?\d[0-9.]*),(-?\d[0-9.]*),(\d{1,2})[.z]/;
const dataLatLngRegex = /!3d(-?[0-9]{1,}.[0-9]{1,})!4d(-?[0-9]{1,}.[0-9]{1,})/;
const placeRegex = /\/place\/(.*)\//;
@ -67,7 +70,6 @@ function redirect(url, initiator) {
traffic: "S", // not implemented on OSM, default to standard.
bicycling: "C",
};
function addressToLatLng(address) {
const xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", `https://nominatim.openstreetmap.org/search/${address}?format=json&limit=1`, false);
@ -85,10 +87,6 @@ function redirect(url, initiator) {
console.info("Error: Status is " + xmlhttp.status);
}
if (disableMaps) return;
if (initiator && initiator.host === "earth.google.com") return;
if (!url.href.match(targets)) return;
let randomInstance;
if (mapsFrontend == 'osm') randomInstance = utils.getRandomInstance(redirects.osm.normal);
if (mapsFrontend == 'facil') randomInstance = utils.getRandomInstance([...facilNormalRedirectsChecks, ...facilNormalCustomRedirects]);

View File

@ -283,78 +283,68 @@ async function testLatency(element, instances) {
resolve(myList);
})
}
// Complete on getting cookies working in Tor, maybe delete all the other same name cookies to prevent overlapping, see ya :)
function copyCookie(frontend, targetUrl, urls, name) {
return new Promise(resolve => {
let query;
if (window.chrome) query = { url: protocolHost(targetUrl), name: name }
else query = { url: protocolHost(targetUrl), name: name, firstPartyDomain: null }
browser.cookies.getAll(
query,
async cookies => {
function setCookie(url, name, value, expirationDate, firstPartyIsolate) {
return new Promise(resolve => {
let query;
if (window.chrome) query = {
url: url, name: name, value: value, secure: true,
expirationDate: expirationDate,
};
else query = {
url: url, name: name, value: value, secure: true,
firstPartyDomain: firstPartyIsolate.value ? new URL(url).hostname : '',
expirationDate: firstPartyIsolate.value ? null : expirationDate,
};
browser.cookies.set(query, () => resolve())
})
}
if (window.chrome) {
browser.storage.local.get('firstPartyIsolate', r => {
console.log('r.firstPartyIsolate', r.firstPartyIsolate);
let query;
if (!r.firstPartyIsolate) query = { url: protocolHost(targetUrl), name: name }
else query = { url: protocolHost(targetUrl), name: name, firstPartyDomain: null }
browser.cookies.getAll(
query,
cookies => {
for (const cookie of cookies)
if (cookie.name == name) {
console.log('cookie', cookie);
for (const url of urls) await setCookie(url, cookie.name, cookie.value, cookie.expirationDate)
browser.storage.local.set({ [`${frontend}_${name}`]: cookie }, () => resolve())
for (const url of urls) {
let setQuery;
let removeQuery;
if (!r.firstPartyIsolate) {
removeQuery = { url: url, name: name };
setQuery = {
url: url, name: name, value: cookie.value, secure: true,
expirationDate: cookie.expirationDate,
};
}
else {
removeQuery = { url: url, name: name, firstPartyDomain: new URL(url).hostname };
setQuery = {
url: url, name: name, value: cookie.value, secure: true,
firstPartyDomain: new URL(url).hostname,
};
}
browser.cookies.remove(removeQuery, () => {
browser.cookies.set(setQuery, () => {
browser.storage.local.set({ [`${frontend}_${name}`]: cookie }, () => resolve())
})
});
}
break;
}
resolve();
} else {
browser.privacy.websites.firstPartyIsolate.get({},
async firstPartyIsolate => {
for (const cookie of cookies)
if (cookie.name == name) {
console.log('cookie', cookie);
for (const url of urls) await setCookie(url, cookie.name, cookie.value, cookie.expirationDate, firstPartyIsolate)
browser.storage.local.set({ [`${frontend}_${name}`]: cookie }, () => resolve())
break;
}
resolve();
}
)
}
});
);
})
})
}
function getCookiesFromStorage(frontend, urls, name) {
let key = `${frontend}_${name}`;
browser.storage.local.get(
key,
r => {
const cookie = r[key];
if (cookie !== undefined)
browser.privacy.websites.firstPartyIsolate.get({},
firstPartyIsolate => {
for (const url of urls)
browser.cookies.set({
url: url,
name: cookie.name,
value: cookie.value,
secure: true,
expirationDate: firstPartyIsolate.value ? null : cookie.expirationDate,
firstPartyDomain: firstPartyIsolate.value ? new URL(url).hostname : '',
})
})
browser.storage.local.get([key, 'firstPartyIsolate'], r => {
const cookie = r[key];
if (cookie === undefined) return;
let query;
if (!r.firstPartyIsolate) query = {
url: url, name: cookie.name, value: cookie.value, secure: true,
expirationDate: cookie.expirationDate,
};
else query = {
url: url, name: cookie.name, value: cookie.value, secure: true,
expirationDate: null,
firstPartyDomain: new URL(url).hostname,
}
)
for (const url of urls) browser.cookies.set(query)
})
}
function copyRaw(test, copyRawElement) {

View File

@ -25,7 +25,6 @@
"storage",
"unlimitedStorage",
"cookies",
"privacy",
"clipboardWrite",
"contextMenus",
"<all_urls>"

View File

@ -67,6 +67,10 @@
<option value="dark" data-localise="__MSG_dark__">Dark</option>
</select>
</div>
<div class="some-block option-block">
<h4>First-party isolation (Enable for Tor)</h4>
<input id="firstPartyIsolate" type="checkbox">
</div>
<div class="some-block option-block">
<h4 data-localise="__MSG_autoRedirect__"></h4>
<input id="auto-redirect" type="checkbox">

View File

@ -1,12 +1,3 @@
import youtubeHelper from "../../assets/javascripts/youtube/youtube.js";
import twitterHelper from "../../assets/javascripts/twitter.js";
import redditHelper from "../../assets/javascripts/reddit.js";
import searchHelper from "../../assets/javascripts/search.js";
import translateHelper from "../../assets/javascripts/translate/translate.js";
import wikipediaHelper from "../../assets/javascripts/wikipedia.js";
import tiktokHelper from "../../assets/javascripts/tiktok.js";
for (const a of document.getElementById('links').getElementsByTagName('a')) {
a.addEventListener('click', e => {
const path = a.getAttribute('href').replace('#', '');

View File

@ -176,16 +176,21 @@ for (const frontend of generalHelper.allPopupFrontends)
}
)
const firstPartyIsolate = document.getElementById('firstPartyIsolate');
firstPartyIsolate.addEventListener("change", () => browser.storage.local.set({ firstPartyIsolate: firstPartyIsolate.checked }))
browser.storage.local.get(
[
'theme',
'autoRedirect',
'exceptions'
'exceptions',
'firstPartyIsolate'
],
r => {
autoRedirectElement.checked = r.autoRedirect;
themeElement.value = r.theme;
firstPartyIsolate.checked = r.firstPartyIsolate;
instanceTypeElement.addEventListener("change",
event => {
instanceType = event.target.options[instanceTypeElement.selectedIndex].value

View File

@ -10,6 +10,10 @@ section#general_page.option-block
option(value="light" data-localise="__MSG_light__") Light
option(value="dark" data-localise="__MSG_dark__") Dark
.some-block.option-block
h4() First-party isolation (Enable for Tor)
input#firstPartyIsolate(type="checkbox")
.some-block.option-block
h4(data-localise="__MSG_autoRedirect__")
input#auto-redirect(type="checkbox")
@ -39,7 +43,7 @@ section#general_page.option-block
x(data-localise="__MSG_updateInstances__") Update Instances
|&nbsp; &nbsp;
.buttons.buttons-inline
label#import_settings_text.button.button-inline(for="import-settings")