mirror of
https://github.com/nileshtrivedi/better
synced 2025-01-28 04:00:39 +01:00
feat: multiple local lists
This commit is contained in:
parent
68d7518a82
commit
e17207a5f1
@ -1,54 +1,74 @@
|
|||||||
var DEFAULT_LIST_URL = 'https://cdn.jsdelivr.net/gh/nileshtrivedi/better/defaultlist.json'
|
var DEFAULT_LIST_URL =
|
||||||
|
"https://cdn.jsdelivr.net/gh/nileshtrivedi/better/defaultlist.json";
|
||||||
var BETTER_ALTERNATIVES = [];
|
var BETTER_ALTERNATIVES = [];
|
||||||
|
|
||||||
chrome.runtime.onInstalled.addListener(function() {
|
chrome.runtime.onInstalled.addListener(function () {
|
||||||
console.log('onInstalled....');
|
console.log("onInstalled....");
|
||||||
onStartup();
|
onStartup();
|
||||||
});
|
});
|
||||||
|
|
||||||
// fetch and save data when chrome restarted
|
// fetch and save data when chrome restarted
|
||||||
chrome.runtime.onStartup.addListener(() => {
|
chrome.runtime.onStartup.addListener(() => {
|
||||||
console.log('onStartup....');
|
console.log("onStartup....");
|
||||||
onStartup();
|
onStartup();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function fetchAllLists(listUrls) {
|
||||||
|
var promises = listUrls.map((listUrl) =>
|
||||||
|
fetch(listUrl)
|
||||||
|
.then((resp) => resp.json())
|
||||||
|
.catch((e) => console.log("List errored out", e))
|
||||||
|
);
|
||||||
|
|
||||||
function onStartup(){
|
Promise.all(promises).then((results) => {
|
||||||
chrome.storage.sync.get(['betterSourceURL'], function(result) {
|
BETTER_ALTERNATIVES = results;
|
||||||
console.log('Value currently is ' + (result.betterSourceURL || DEFAULT_LIST_URL));
|
chrome.storage.local.set({ betterSourceData: results }, function () {
|
||||||
|
console.log("Set betterSource");
|
||||||
var listUrl = result.betterSourceURL || DEFAULT_LIST_URL
|
|
||||||
// Uncomment this when testing list changes locally
|
|
||||||
listUrl = "/defaultlist.json"
|
|
||||||
fetch(listUrl)
|
|
||||||
.then(response => response.json())
|
|
||||||
.then(data => {
|
|
||||||
console.log("Got data: ", data);
|
|
||||||
BETTER_ALTERNATIVES = data;
|
|
||||||
chrome.storage.local.set({betterSourceData: data}, function() {
|
|
||||||
console.log('Set betterSource = ' + data);
|
|
||||||
})
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function getMatch(url){
|
function onStartup() {
|
||||||
var match = BETTER_ALTERNATIVES.find(pattern => url.match(new RegExp(pattern.urlPattern)));
|
chrome.storage.sync.get(["betterSourceURL"], function (result) {
|
||||||
if(match && match.alternatives)
|
var listUrl = result.betterSourceURL || DEFAULT_LIST_URL;
|
||||||
return match;
|
// Uncomment this when testing list changes locally
|
||||||
else return null;
|
// listUrl = "/defaultlist.json";
|
||||||
|
fetchAllLists([listUrl, "/lists/t1.json", "/lists/t2.json"]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
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) => {
|
chrome.runtime.onMessage.addListener((msg, sender, response) => {
|
||||||
switch (msg.type) {
|
switch (msg.type) {
|
||||||
case 'getMatch':
|
case "getMatch":
|
||||||
response(getMatch(msg.url));
|
response(getMatch(msg.url));
|
||||||
break;
|
break;
|
||||||
case 'reloadList':
|
case "reloadList":
|
||||||
onStartup();
|
onStartup();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
response('unknown request');
|
response("unknown request");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
});
|
});
|
12
lists/t1.json
Normal file
12
lists/t1.json
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"urlPattern": "https://(www.)?miteshshah.com",
|
||||||
|
"alternatives": [
|
||||||
|
{
|
||||||
|
"url": "https://oxal.org",
|
||||||
|
"name": "Mitesh",
|
||||||
|
"desc": "My other website"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
12
lists/t2.json
Normal file
12
lists/t2.json
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"urlPattern": "https://(www.)?miteshshah.com",
|
||||||
|
"alternatives": [
|
||||||
|
{
|
||||||
|
"url": "https://computableverse.com",
|
||||||
|
"name": "Miteshs Blog",
|
||||||
|
"desc": "My another tech blog"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
@ -27,5 +27,5 @@
|
|||||||
"page": "options.html",
|
"page": "options.html",
|
||||||
"open_in_tab": false
|
"open_in_tab": false
|
||||||
},
|
},
|
||||||
"web_accessible_resources": ["defaultlist.json"]
|
"web_accessible_resources": ["defaultlist.json", "lists/*"]
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user