add Wikiless support

This commit is contained in:
Kotuklion 2021-07-12 18:16:23 +02:00
parent 8a21b62478
commit 60be141050
7 changed files with 149 additions and 1 deletions

View File

@ -35,6 +35,10 @@
"message": "SimplyTranslate Instance",
"description": "Label for SimplyTranslate instance field option (options)."
},
"wikipediaInstance": {
"message": "Wikipedia Instance",
"description": "Label for Wikipedia instance field option (options)."
},
"disableNitter": {
"message": "Nitter Redirects",
"description": "Label for enable/disable Nitter redirects option (options & pop-up)."
@ -63,6 +67,10 @@
"message": "SimplyTranslate Redirects",
"description": "Label for enable/disable SimplyTranslate redirects option (options & pop-up)."
},
"disableWikipedia": {
"message": "Wikipedia Redirects",
"description": "Label for enable/disable Wikipedia redirects option (options & pop-up)."
},
"alwaysProxy": {
"message": "Always proxy videos through Invidious",
"description": "Label for 'Always proxy videos through Invidious' option (options)."

View File

@ -0,0 +1,8 @@
const targets = /wikipedia.org/;
const redirects = ["https://wikiless.org"];
export default {
targets,
redirects,
};

View File

@ -8,6 +8,7 @@ import mapsHelper from "../../assets/javascripts/helpers/google-maps.js";
import redditHelper from "../../assets/javascripts/helpers/reddit.js";
import searchHelper from "../../assets/javascripts/helpers/google-search.js";
import googleTranslateHelper from "../../assets/javascripts/helpers/google-translate.js";
import wikipediaHelper from "../../assets/javascripts/helpers/wikipedia.js";
const nitterInstances = twitterHelper.redirects;
const twitterDomains = twitterHelper.targets;
@ -33,6 +34,9 @@ const searchEngineInstances = searchHelper.redirects;
const simplyTranslateInstances = googleTranslateHelper.redirects;
const simplyTranslateDefault = simplyTranslateInstances[0];
const googleTranslateDomains = googleTranslateHelper.targets;
const wikipediaInstances = wikipediaHelper.redirects;
const wikipediaDefault = simplyTranslateInstances[0];
const wikipediaRegex = wikipediaHelper.targets;
let disableNitter;
let disableInvidious;
@ -41,6 +45,7 @@ let disableOsm;
let disableReddit;
let disableSearchEngine;
let disableSimplyTranslate;
let disableWikipedia;
let nitterInstance;
let invidiousInstance;
let bibliogramInstance;
@ -48,6 +53,7 @@ let osmInstance;
let redditInstance;
let searchEngineInstance;
let simplyTranslateInstance;
let wikipediaInstance;
let alwaysProxy;
let onlyEmbeddedVideo;
let videoQuality;
@ -73,6 +79,7 @@ browser.storage.sync.get(
"redditInstance",
"searchEngineInstance",
"simplyTranslateInstance",
"wikipediaInstance",
"disableNitter",
"disableInvidious",
"disableBibliogram",
@ -80,6 +87,7 @@ browser.storage.sync.get(
"disableReddit",
"disableSearchEngine",
"disableSimplyTranslate",
"disableWikipedia",
"alwaysProxy",
"onlyEmbeddedVideo",
"videoQuality",
@ -103,12 +111,14 @@ browser.storage.sync.get(
searchEngineInstance = result.searchEngineInstance;
simplyTranslateInstance =
result.simplyTranslateInstance || simplyTranslateDefault;
wikipediaInstance = result.wikipediaInstance || wikipediaDefault;
disableNitter = result.disableNitter;
disableInvidious = result.disableInvidious;
disableBibliogram = result.disableBibliogram;
disableOsm = result.disableOsm;
disableReddit = result.disableReddit;
disableSearchEngine = result.disableSearchEngine;
disableWikipedia = result.disableWikipedia;
disableSimplyTranslate = result.disableSimplyTranslate;
alwaysProxy = result.alwaysProxy;
onlyEmbeddedVideo = result.onlyEmbeddedVideo;
@ -153,6 +163,11 @@ browser.storage.onChanged.addListener((changes) => {
simplyTranslateInstance =
changes.simplyTranslateInstance.newValue || simplyTranslateDefault;
}
if ("wikipediaInstance" in changes) {
wikipediaInstance =
changes.wikipediaInstance.newValue || wikipediaDefault;
}
if ("redditInstance" in changes) {
redditInstance = changes.redditInstance.newValue || redditDefault;
}
@ -180,6 +195,9 @@ browser.storage.onChanged.addListener((changes) => {
if ("disableSimplyTranslate" in changes) {
disableSimplyTranslate = changes.disableSimplyTranslate.newValue;
}
if ("disableWikipedia" in changes) {
disableWikipedia = changes.disableWikipedia.newValue;
}
if ("alwaysProxy" in changes) {
alwaysProxy = changes.alwaysProxy.newValue;
}
@ -525,16 +543,40 @@ function redirectGoogleTranslate(url, initiator) {
return `${simplyTranslateInstance}/${url.search}`;
}
function redirectWikipedia(url, initiator) {
if (disableWikipedia || isException(url, initiator)) {
return null;
}
let link = `${wikipediaInstance}${url.pathname}`;
let urlSplit = url.host.split('.');
if (urlSplit[0] != "wikipedia" && urlSplit[0] != "www") {
if (urlSplit[0] == 'm')
link += "?mobileaction=toggle_view_mobile";
else
link += `?lang=${urlSplit[0]}`;
if (urlSplit[1] == 'm')
link += "&mobileaction=toggle_view_mobile";
//wikiless doesn't have mobile view support yet
}
if (urlSplit[urlSplit.length - 1] == "org" &&
urlSplit[urlSplit.length - 2] == "wikipedia")
//just in case someone wanted to visit wikipedia.org.foo.bar.net
return link;
}
browser.webRequest.onBeforeRequest.addListener(
(details) => {
const url = new URL(details.url);
let initiator;
if (details.originUrl) {
initiator = new URL(details.originUrl);
} else if (details.initiator) {
initiator = new URL(details.initiator);
}
let redirect;
if (youtubeDomains.includes(url.host)) {
redirect = {
redirectUrl: redirectYouTube(url, initiator, details.type),
@ -563,6 +605,10 @@ browser.webRequest.onBeforeRequest.addListener(
redirect = {
redirectUrl: redirectGoogleTranslate(url, initiator),
};
} else if (url.href.match(wikipediaRegex)) {
redirect = {
redirectUrl: redirectWikipedia(url, initiator),
};
}
if (redirect && redirect.redirectUrl) {
console.info(

View File

@ -183,6 +183,25 @@
</tbody>
</table>
</section>
<section class="settings-block">
<table class="option" aria-label="Toggle Wikipedia redirects">
<tbody>
<tr>
<td>
<h1 data-localise="__MSG_disableWikipedia__">Wikipedia Redirects</h1>
</td>
<td>
<input
aria-hidden="true"
id="disable-wikipedia"
type="checkbox"
/>&nbsp;
<label for="disable-wikipedia" class="checkbox-label"></label>
</td>
</tr>
</tbody>
</table>
</section>
<section class="settings-block">
<h1 data-localise="__MSG_nitterInstance__">Nitter Instance</h1>
<div class="autocomplete">
@ -258,6 +277,16 @@
/>
</div>
</section>
<section class="settings-block">
<h1 data-localise="__MSG_wikipediaInstance__">Wikipedia Instance</h1>
<div class="autocomplete">
<input
id="wikipedia-instance"
type="url"
placeholder="https://wikiless.org"
/>
</div>
</section>
<section class="settings-block">
<h1 data-localise="__MSG_theme__">Theme</h1>
<select id="theme">

View File

@ -8,6 +8,7 @@ import mapsHelper from "../../assets/javascripts/helpers/google-maps.js";
import redditHelper from "../../assets/javascripts/helpers/reddit.js";
import searchHelper from "../../assets/javascripts/helpers/google-search.js";
import googleTranslateHelper from "../../assets/javascripts/helpers/google-translate.js";
import wikipediaHelper from "../../assets/javascripts/helpers/wikipedia.js";
const nitterInstances = twitterHelper.redirects;
const invidiousInstances = youtubeHelper.redirects;
@ -16,6 +17,7 @@ const osmInstances = mapsHelper.redirects;
const redditInstances = redditHelper.redirects;
const searchEngineInstances = searchHelper.redirects;
const simplyTranslateInstances = googleTranslateHelper.redirects;
const wikipediaInstances = wikipediaHelper.redirects;
const autocompletes = [
{ id: "nitter-instance", instances: nitterInstances },
{ id: "invidious-instance", instances: invidiousInstances },
@ -27,6 +29,7 @@ const autocompletes = [
instances: searchEngineInstances.map((instance) => instance.link),
},
{ id: "simply-translate-instance", instances: simplyTranslateInstances },
{ id: "wikipedia-instance", instances: wikipediaInstances },
];
const domparser = new DOMParser();
@ -39,6 +42,7 @@ let searchEngineInstance = document.getElementById("search-engine-instance");
let simplyTranslateInstance = document.getElementById(
"simply-translate-instance"
);
let wikipediaInstance = document.getElementById("wikipedia-instance");
let disableNitter = document.getElementById("disable-nitter");
let disableInvidious = document.getElementById("disable-invidious");
let disableBibliogram = document.getElementById("disable-bibliogram");
@ -48,6 +52,7 @@ let disableSearchEngine = document.getElementById("disable-search-engine");
let disableSimplyTranslate = document.getElementById(
"disable-simply-translate"
);
let disableWikipedia = document.getElementById("disable-wikipedia");
let alwaysProxy = document.getElementById("always-proxy");
let onlyEmbeddedVideo = document.getElementById("only-embed");
let videoQuality = document.getElementById("video-quality");
@ -100,6 +105,7 @@ browser.storage.sync.get(
"redditInstance",
"searchEngineInstance",
"simplyTranslateInstance",
"wikipediaInstance",
"disableNitter",
"disableInvidious",
"disableBibliogram",
@ -107,6 +113,7 @@ browser.storage.sync.get(
"disableReddit",
"disableSearchEngine",
"disableSimplyTranslate",
"disableWikipedia",
"alwaysProxy",
"onlyEmbeddedVideo",
"videoQuality",
@ -135,6 +142,7 @@ browser.storage.sync.get(
searchEngineInstance.value =
(result.searchEngineInstance && result.searchEngineInstance.link) || "";
simplyTranslateInstance.value = result.simplyTranslateInstance || "";
wikipediaInstance.value = result.wikipediaInstance || "";
disableNitter.checked = !result.disableNitter;
disableInvidious.checked = !result.disableInvidious;
disableBibliogram.checked = !result.disableBibliogram;
@ -142,6 +150,7 @@ browser.storage.sync.get(
disableReddit.checked = !result.disableReddit;
disableSearchEngine.checked = !result.disableSearchEngine;
disableSimplyTranslate.checked = !result.disableSimplyTranslate;
disableWikipedia.checked = !result.disableWikipedia;
alwaysProxy.checked = result.alwaysProxy;
onlyEmbeddedVideo.checked = result.onlyEmbeddedVideo;
videoQuality.value = result.videoQuality || "";
@ -328,6 +337,18 @@ simplyTranslateInstance.addEventListener(
simplyTranslateInstanceChange
);
const wikipediaInstanceChange = debounce(() => {
if (wikipediaInstance.checkValidity()) {
browser.storage.sync.set({
wikipediaInstance: parseURL(wikipediaInstance.value),
});
}
}, 500);
wikipediaInstance.addEventListener(
"input",
wikipediaInstanceChange
);
disableNitter.addEventListener("change", (event) => {
browser.storage.sync.set({ disableNitter: !event.target.checked });
});
@ -356,6 +377,10 @@ disableSimplyTranslate.addEventListener("change", (event) => {
browser.storage.sync.set({ disableSimplyTranslate: !event.target.checked });
});
disableWikipedia.addEventListener("change", (event) => {
browser.storage.sync.set({ disableWikipedia: !event.target.checked });
});
alwaysProxy.addEventListener("change", (event) => {
browser.storage.sync.set({ alwaysProxy: event.target.checked });
});

View File

@ -185,6 +185,31 @@
</table>
</section>
<section class="settings-block">
<table class="option" aria-label="Toggle Wikiepdia redirects">
<tbody>
<tr>
<td>
<h1 data-localise="__MSG_disableWikipedia__">
Wikipedia Redirects
</h1>
</td>
<td>
<input
aria-hidden="true"
id="disable-wikipedia"
type="checkbox"
/>&nbsp;
<label
for="disable-wikipedia"
class="checkbox-label"
></label>
</td>
</tr>
</tbody>
</table>
</section>
<section class="settings-block"></section>
<footer>

View File

@ -7,6 +7,7 @@ let disableOsm = document.querySelector("#disable-osm");
let disableReddit = document.querySelector("#disable-reddit");
let disableSearchEngine = document.querySelector("#disable-searchEngine");
let disableSimplyTranslate = document.querySelector("#disable-simplyTranslate");
let disableWikipedia = document.querySelector("#disable-wikipedia");
let version = document.querySelector("#version");
window.browser = window.browser || window.chrome;
@ -20,6 +21,7 @@ browser.storage.sync.get(
"disableReddit",
"disableSearchEngine",
"disableSimplyTranslate",
"disableWikipedia",
"theme",
],
(result) => {
@ -31,6 +33,7 @@ browser.storage.sync.get(
disableReddit.checked = !result.disableReddit;
disableSearchEngine.checked = !result.disableSearchEngine;
disableSimplyTranslate.checked = !result.disableSimplyTranslate;
disableWikipedia.checked = !result.disableWikipedia;
}
);
@ -64,6 +67,10 @@ disableSimplyTranslate.addEventListener("change", (event) => {
browser.storage.sync.set({ disableSimplyTranslate: !event.target.checked });
});
disableWikipedia.addEventListener("change", (event) => {
browser.storage.sync.set({ disableWikipedia: !event.target.checked });
});
document.querySelector("#more-options").addEventListener("click", () => {
browser.runtime.openOptionsPage();
});