mirror of
https://github.com/nileshtrivedi/better
synced 2025-01-01 07:57:24 +01:00
1c4e6ffcfb
* feat: options to add, edit, remove source lists * fix: failure recovery even if one or more sources fail
78 lines
2.0 KiB
JavaScript
78 lines
2.0 KiB
JavaScript
let DEFAULT_LIST_URL =
|
|
"https://cdn.jsdelivr.net/gh/nileshtrivedi/better/defaultlist.json";
|
|
var BETTER_ALTERNATIVES = [];
|
|
|
|
chrome.runtime.onInstalled.addListener(function () {
|
|
console.log("onInstalled....");
|
|
onStartup();
|
|
});
|
|
|
|
// fetch and save data when chrome restarted
|
|
chrome.runtime.onStartup.addListener(() => {
|
|
console.log("onStartup....");
|
|
onStartup();
|
|
});
|
|
|
|
function fetchAllLists(listUrls) {
|
|
var promises = listUrls.map(
|
|
(listUrl) => fetch(listUrl).then((resp) => resp.json())
|
|
// .catch((e) => console.log("List errored out", e))
|
|
);
|
|
|
|
// wait for all requests to complete, ignore errors
|
|
Promise.all(promises.map((p) => p.catch((e) => null))).then((results) => {
|
|
BETTER_ALTERNATIVES = results.filter((item) => item != null);
|
|
chrome.storage.local.set({ betterSourceData: results }, function () {
|
|
console.log("Set betterSource");
|
|
});
|
|
});
|
|
}
|
|
|
|
function onStartup() {
|
|
chrome.storage.sync.get(["betterSourceUrls"], function (result) {
|
|
let betterSourceUrls = [];
|
|
if (result && result.betterSourceUrls) {
|
|
betterSourceUrls = JSON.parse(result.betterSourceUrls);
|
|
}
|
|
if (betterSourceUrls.length === 0) {
|
|
betterSourceUrls = [DEFAULT_LIST_URL];
|
|
}
|
|
fetchAllLists(betterSourceUrls);
|
|
});
|
|
}
|
|
|
|
function getMatch(url) {
|
|
var matches = BETTER_ALTERNATIVES.reduce((result, sourceList) => {
|
|
var match = sourceList.find((pattern) =>
|
|
url.match(new RegExp(pattern.urlPattern))
|
|
);
|
|
if (match && match.alternatives) result.push(match);
|
|
return result;
|
|
}, []);
|
|
|
|
if (matches.length) {
|
|
var combinedMatch = {
|
|
urlPattern: matches[0].urlPattern,
|
|
alternatives: matches.reduce(
|
|
(result, match) => result.concat(match.alternatives),
|
|
[]
|
|
),
|
|
};
|
|
return combinedMatch;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
chrome.runtime.onMessage.addListener((msg, sender, response) => {
|
|
switch (msg.type) {
|
|
case "getMatch":
|
|
response(getMatch(msg.url));
|
|
break;
|
|
case "reloadList":
|
|
onStartup();
|
|
break;
|
|
default:
|
|
response("unknown request");
|
|
break;
|
|
}
|
|
}); |