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() { chrome.runtime.onInstalled.addListener(function() {
console.log('onInstalled....'); 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(() => { chrome.runtime.onStartup.addListener(() => {
console.log('onStartup....'); console.log('onStartup....');
onStartup(); onStartup();
@ -29,7 +30,7 @@ function onStartup(){
} }
function getMatch(url){ 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) if(match)
return match[1]; return match[1];
else return null; else return null;
@ -40,6 +41,9 @@ chrome.runtime.onMessage.addListener((msg, sender, response) => {
case 'getMatch': case 'getMatch':
response(getMatch(msg.url)); response(getMatch(msg.url));
break; break;
case 'reloadList':
onStartup();
break;
default: default:
response('unknown request'); response('unknown request');
break; break;

View File

@ -11,6 +11,9 @@ TODO
- 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.
- Since we're replacing innerHTML, we should protect against XSS attacks. - 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) { function showBetter(alternative) {
@ -29,8 +32,6 @@ function showBetter(alternative) {
document.body.appendChild(betterdiv); document.body.appendChild(betterdiv);
} }
showBetter(findBetter(document.location.href));
chrome.runtime.sendMessage({type: 'getMatch', url: document.location.href}, (response) => { chrome.runtime.sendMessage({type: 'getMatch', url: document.location.href}, (response) => {
if (response) { if (response) {
showBetter(response); showBetter(response);

View File

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

View File

@ -30,7 +30,8 @@ button {
<body> <body>
<div> <div>
<h3>Choose a different source of suggestions!</h3> <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> <button id="betterSourceSubmit" value="Save">Save</button>
</div> </div>
</body> </body>

View File

@ -4,5 +4,11 @@ let submit = document.getElementById('betterSourceSubmit');
submit.addEventListener('click', function() { submit.addEventListener('click', function() {
chrome.storage.sync.set({betterSourceURL: input.value}, 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");
}
});
}); });