1
0
mirror of https://github.com/nileshtrivedi/better synced 2025-06-27 09:03:11 +02:00

WIP: Fetch list from URL

This commit is contained in:
Nilesh
2020-08-02 23:04:43 +05:30
parent af891cb527
commit e284d5c6ad
4 changed files with 61 additions and 17 deletions

View File

@@ -1,5 +1,47 @@
var DEFAULT_LIST_URL = 'https://cdn.jsdelivr.net/gh/nileshtrivedi/better/defaultlist.json'
var BETTER_ALTERNATIVES = [];
chrome.runtime.onInstalled.addListener(function() {
// chrome.storage.sync.set({color: '#3aa757'}, function() {
// console.log("The color is green.");
// });
});
console.log('onInstalled....');
});
// fetch and save data when chrome restarted, alarm will continue running when chrome is restarted
chrome.runtime.onStartup.addListener(() => {
console.log('onStartup....');
onStartup();
});
function onStartup(){
chrome.storage.sync.get(['betterSourceURL'], function(result) {
console.log('Value currently is ' + (result.betterSourceURL || DEFAULT_LIST_URL));
fetch((result.betterSourceURL || DEFAULT_LIST_URL))
.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){
var match = BETTER_ALTERNATIVES.find(pattern => url.match(new Regexp(pattern[0])));
if(match)
return match[1];
else return null;
}
chrome.runtime.onMessage.addListener((msg, sender, response) => {
switch (msg.type) {
case 'getMatch':
response(getMatch(msg.url));
break;
default:
response('unknown request');
break;
}
});