1
0
mirror of https://github.com/nileshtrivedi/better synced 2024-12-27 13:42:26 +01:00

Fix remote list loading

This commit is contained in:
Nilesh 2020-08-02 23:30:38 +05:30
parent e284d5c6ad
commit 2d5d6a2f4d
5 changed files with 23 additions and 8 deletions

View File

@ -3,9 +3,10 @@ var BETTER_ALTERNATIVES = [];
chrome.runtime.onInstalled.addListener(function() {
console.log('onInstalled....');
onStartup();
});
// fetch and save data when chrome restarted, alarm will continue running when chrome is restarted
// fetch and save data when chrome restarted
chrome.runtime.onStartup.addListener(() => {
console.log('onStartup....');
onStartup();
@ -29,7 +30,7 @@ function onStartup(){
}
function getMatch(url){
var match = BETTER_ALTERNATIVES.find(pattern => url.match(new Regexp(pattern[0])));
var match = BETTER_ALTERNATIVES.find(pattern => url.match(new RegExp(pattern[0])));
if(match)
return match[1];
else return null;
@ -40,6 +41,9 @@ chrome.runtime.onMessage.addListener((msg, sender, response) => {
case 'getMatch':
response(getMatch(msg.url));
break;
case 'reloadList':
onStartup();
break;
default:
response('unknown request');
break;

View File

@ -11,6 +11,9 @@ TODO
- 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.
- Since we're replacing innerHTML, we should protect against XSS attacks.
- Fix and test the options UI
- Test and fix for Chrome, Brave & Firefox
*/
function showBetter(alternative) {
@ -29,8 +32,6 @@ function showBetter(alternative) {
document.body.appendChild(betterdiv);
}
showBetter(findBetter(document.location.href));
chrome.runtime.sendMessage({type: 'getMatch', url: document.location.href}, (response) => {
if (response) {
showBetter(response);

View File

@ -14,5 +14,8 @@
}
],
"manifest_version": 2,
"options_page": "options.html"
"options_ui": {
"page": "options.html",
"open_in_tab": false
}
}

View File

@ -30,7 +30,8 @@ button {
<body>
<div>
<h3>Choose a different source of suggestions!</h3>
<input id="betterSourceText" type="url" placeholder="https://github.com/nileshtrivedi/better/list.json" />
<!-- TODO: Fetch the current option value and set in URL field below -->
<input id="betterSourceText" type="url" placeholder="https://cdn.jsdelivr.net/gh/nileshtrivedi/better/defaultlist.json" />
<button id="betterSourceSubmit" value="Save">Save</button>
</div>
</body>

View File

@ -3,6 +3,12 @@ let submit = document.getElementById('betterSourceSubmit');
submit.addEventListener('click', function() {
chrome.storage.sync.set({betterSourceURL: input.value}, function() {
console.log('Set betterSource = ' + input.value);
})
console.log('Set betterSource = ' + input.value);
});
chrome.runtime.sendMessage({type: 'reloadList', url: input.value}, (response) => {
if (response) {
console.log("BETTER_ALTERNATIVES list is reloaded");
}
});
});