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:
@ -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.runtime.onInstalled.addListener(function() {
|
||||||
// chrome.storage.sync.set({color: '#3aa757'}, function() {
|
console.log('onInstalled....');
|
||||||
// console.log("The color is green.");
|
});
|
||||||
// });
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
});
|
});
|
@ -1,25 +1,18 @@
|
|||||||
// document.body.style.border = "15px solid red";
|
// document.body.style.border = "15px solid red";
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
|
IN PROGRESS:
|
||||||
|
|
||||||
|
- List of alternatives should be loaded from a user-specified external source. Can be modified via options.html/options.js
|
||||||
|
|
||||||
TODO
|
TODO
|
||||||
- Pop-up should be formatted to look better.
|
- Pop-up should be formatted to look better.
|
||||||
- Pop-up should be dismissable (per URL / per domain).
|
- Pop-up should be dismissable (per URL / per domain).
|
||||||
- Once dismissed, popup should not be shown on the same url/domain. Use cookies or localStorage for this.
|
- Once dismissed, popup should not be shown on the same url/domain. Use cookies or localStorage for this.
|
||||||
- List of alternatives should be loaded from a user-specified external source. Can be modified via options.html/options.js
|
|
||||||
- Since we're replacing innerHTML, we should protect against XSS attacks.
|
- Since we're replacing innerHTML, we should protect against XSS attacks.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
BETTER_ALTERNATIVES = [
|
|
||||||
[ /https?:\/\/(www.)?google.com\/chrome/ , "A better alternative is <a href='https://mozilla.com' target='_blank'>Mozilla</a>."]
|
|
||||||
]
|
|
||||||
|
|
||||||
function findBetter(url) {
|
|
||||||
var match = BETTER_ALTERNATIVES.find(pattern => url.match(pattern[0]));
|
|
||||||
if(match)
|
|
||||||
return match[1];
|
|
||||||
else return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function showBetter(alternative) {
|
function showBetter(alternative) {
|
||||||
if(!alternative) return;
|
if(!alternative) return;
|
||||||
let betterdiv = document.createElement("div");
|
let betterdiv = document.createElement("div");
|
||||||
@ -37,3 +30,9 @@ function showBetter(alternative) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
showBetter(findBetter(document.location.href));
|
showBetter(findBetter(document.location.href));
|
||||||
|
|
||||||
|
chrome.runtime.sendMessage({type: 'getMatch', url: document.location.href}, (response) => {
|
||||||
|
if (response) {
|
||||||
|
showBetter(response);
|
||||||
|
}
|
||||||
|
});
|
3
defaultlist.json
Normal file
3
defaultlist.json
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
[
|
||||||
|
[ "https?:\/\/(www.)?google.com\/chrome" , "A better alternative is <a href='https://mozilla.com' target='_blank'>Mozilla</a>."]
|
||||||
|
]
|
@ -2,7 +2,7 @@ let input = document.getElementById('betterSourceText');
|
|||||||
let submit = document.getElementById('betterSourceSubmit');
|
let submit = document.getElementById('betterSourceSubmit');
|
||||||
|
|
||||||
submit.addEventListener('click', function() {
|
submit.addEventListener('click', function() {
|
||||||
chrome.storage.sync.set({betterSource: input.value}, function() {
|
chrome.storage.sync.set({betterSourceURL: input.value}, function() {
|
||||||
console.log('Set betterSource = ' + input.value);
|
console.log('Set betterSource = ' + input.value);
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user