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", "maps",
], ],
autoRedirect: false, autoRedirect: false,
firstPartyIsolate: false,
}, () => resolve()) }, () => resolve())
) )
} }

View File

@ -46,6 +46,9 @@ init();
browser.storage.onChanged.addListener(init) browser.storage.onChanged.addListener(init)
function redirect(url, initiator) { 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 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 dataLatLngRegex = /!3d(-?[0-9]{1,}.[0-9]{1,})!4d(-?[0-9]{1,}.[0-9]{1,})/;
const placeRegex = /\/place\/(.*)\//; const placeRegex = /\/place\/(.*)\//;
@ -67,7 +70,6 @@ function redirect(url, initiator) {
traffic: "S", // not implemented on OSM, default to standard. traffic: "S", // not implemented on OSM, default to standard.
bicycling: "C", bicycling: "C",
}; };
function addressToLatLng(address) { function addressToLatLng(address) {
const xmlhttp = new XMLHttpRequest(); const xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", `https://nominatim.openstreetmap.org/search/${address}?format=json&limit=1`, false); 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); 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; let randomInstance;
if (mapsFrontend == 'osm') randomInstance = utils.getRandomInstance(redirects.osm.normal); if (mapsFrontend == 'osm') randomInstance = utils.getRandomInstance(redirects.osm.normal);
if (mapsFrontend == 'facil') randomInstance = utils.getRandomInstance([...facilNormalRedirectsChecks, ...facilNormalCustomRedirects]); if (mapsFrontend == 'facil') randomInstance = utils.getRandomInstance([...facilNormalRedirectsChecks, ...facilNormalCustomRedirects]);

View File

@ -283,78 +283,68 @@ async function testLatency(element, instances) {
resolve(myList); 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) { function copyCookie(frontend, targetUrl, urls, name) {
return new Promise(resolve => { return new Promise(resolve => {
browser.storage.local.get('firstPartyIsolate', r => {
console.log('r.firstPartyIsolate', r.firstPartyIsolate);
let query; let query;
if (window.chrome) query = { url: protocolHost(targetUrl), name: name } if (!r.firstPartyIsolate) query = { url: protocolHost(targetUrl), name: name }
else query = { url: protocolHost(targetUrl), name: name, firstPartyDomain: null } else query = { url: protocolHost(targetUrl), name: name, firstPartyDomain: null }
browser.cookies.getAll( browser.cookies.getAll(
query, query,
async cookies => { cookies => {
function setCookie(url, name, value, expirationDate, firstPartyIsolate) { for (const cookie of cookies)
return new Promise(resolve => { if (cookie.name == name) {
let query; for (const url of urls) {
if (window.chrome) query = { let setQuery;
url: url, name: name, value: value, secure: true, let removeQuery;
expirationDate: expirationDate, if (!r.firstPartyIsolate) {
removeQuery = { url: url, name: name };
setQuery = {
url: url, name: name, value: cookie.value, secure: true,
expirationDate: cookie.expirationDate,
}; };
else query = { }
url: url, name: name, value: value, secure: true, else {
firstPartyDomain: firstPartyIsolate.value ? new URL(url).hostname : '', removeQuery = { url: url, name: name, firstPartyDomain: new URL(url).hostname };
expirationDate: firstPartyIsolate.value ? null : expirationDate, setQuery = {
url: url, name: name, value: cookie.value, secure: true,
firstPartyDomain: new URL(url).hostname,
}; };
browser.cookies.set(query, () => resolve()) }
browser.cookies.remove(removeQuery, () => {
browser.cookies.set(setQuery, () => {
browser.storage.local.set({ [`${frontend}_${name}`]: cookie }, () => resolve())
}) })
}
if (window.chrome) {
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())
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();
}
)
}
}); });
}
break;
}
resolve();
}
);
})
}) })
} }
function getCookiesFromStorage(frontend, urls, name) { function getCookiesFromStorage(frontend, urls, name) {
let key = `${frontend}_${name}`; let key = `${frontend}_${name}`;
browser.storage.local.get( browser.storage.local.get([key, 'firstPartyIsolate'], r => {
key,
r => {
const cookie = r[key]; const cookie = r[key];
if (cookie !== undefined) if (cookie === undefined) return;
browser.privacy.websites.firstPartyIsolate.get({}, let query;
firstPartyIsolate => { if (!r.firstPartyIsolate) query = {
for (const url of urls) url: url, name: cookie.name, value: cookie.value, secure: true,
browser.cookies.set({ expirationDate: cookie.expirationDate,
url: url, };
name: cookie.name, else query = {
value: cookie.value, url: url, name: cookie.name, value: cookie.value, secure: true,
secure: true, expirationDate: null,
expirationDate: firstPartyIsolate.value ? null : cookie.expirationDate, firstPartyDomain: new URL(url).hostname,
firstPartyDomain: firstPartyIsolate.value ? new URL(url).hostname : '',
})
})
} }
) for (const url of urls) browser.cookies.set(query)
})
} }
function copyRaw(test, copyRawElement) { function copyRaw(test, copyRawElement) {

View File

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

View File

@ -67,6 +67,10 @@
<option value="dark" data-localise="__MSG_dark__">Dark</option> <option value="dark" data-localise="__MSG_dark__">Dark</option>
</select> </select>
</div> </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"> <div class="some-block option-block">
<h4 data-localise="__MSG_autoRedirect__"></h4> <h4 data-localise="__MSG_autoRedirect__"></h4>
<input id="auto-redirect" type="checkbox"> <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')) { for (const a of document.getElementById('links').getElementsByTagName('a')) {
a.addEventListener('click', e => { a.addEventListener('click', e => {
const path = a.getAttribute('href').replace('#', ''); 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( browser.storage.local.get(
[ [
'theme', 'theme',
'autoRedirect', 'autoRedirect',
'exceptions' 'exceptions',
'firstPartyIsolate'
], ],
r => { r => {
autoRedirectElement.checked = r.autoRedirect; autoRedirectElement.checked = r.autoRedirect;
themeElement.value = r.theme; themeElement.value = r.theme;
firstPartyIsolate.checked = r.firstPartyIsolate;
instanceTypeElement.addEventListener("change", instanceTypeElement.addEventListener("change",
event => { event => {
instanceType = event.target.options[instanceTypeElement.selectedIndex].value 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="light" data-localise="__MSG_light__") Light
option(value="dark" data-localise="__MSG_dark__") Dark 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 .some-block.option-block
h4(data-localise="__MSG_autoRedirect__") h4(data-localise="__MSG_autoRedirect__")
input#auto-redirect(type="checkbox") input#auto-redirect(type="checkbox")