Merge pull request #35 from nileshtrivedi/feature-multiple-lists

feat: multiple local lists
This commit is contained in:
Mitesh 2020-09-12 20:50:31 +05:30 committed by GitHub
commit 17a069bee5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 35 deletions

View File

@ -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
View 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
View File

@ -0,0 +1,12 @@
[
{
"urlPattern": "https://(www.)?miteshshah.com",
"alternatives": [
{
"url": "https://computableverse.com",
"name": "Miteshs Blog",
"desc": "My another tech blog"
}
]
}
]

View File

@ -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/*"]
} }