mirror of
https://github.com/nileshtrivedi/better
synced 2024-12-29 14:39:57 +01:00
feat: options to add, edit, remove source lists
This commit is contained in:
parent
17a069bee5
commit
4489a30e50
@ -1,4 +1,4 @@
|
|||||||
var DEFAULT_LIST_URL =
|
let DEFAULT_LIST_URL =
|
||||||
"https://cdn.jsdelivr.net/gh/nileshtrivedi/better/defaultlist.json";
|
"https://cdn.jsdelivr.net/gh/nileshtrivedi/better/defaultlist.json";
|
||||||
var BETTER_ALTERNATIVES = [];
|
var BETTER_ALTERNATIVES = [];
|
||||||
|
|
||||||
@ -29,11 +29,15 @@ function fetchAllLists(listUrls) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onStartup() {
|
function onStartup() {
|
||||||
chrome.storage.sync.get(["betterSourceURL"], function (result) {
|
chrome.storage.sync.get(["betterSourceUrls"], function (result) {
|
||||||
var listUrl = result.betterSourceURL || DEFAULT_LIST_URL;
|
let betterSourceUrls = [];
|
||||||
// Uncomment this when testing list changes locally
|
if (result && result.betterSourceUrls) {
|
||||||
// listUrl = "/defaultlist.json";
|
betterSourceUrls = JSON.parse(result.betterSourceUrls);
|
||||||
fetchAllLists([listUrl, "/lists/t1.json", "/lists/t2.json"]);
|
}
|
||||||
|
if (betterSourceUrls.length === 0) {
|
||||||
|
betterSourceUrls = [DEFAULT_LIST_URL];
|
||||||
|
}
|
||||||
|
fetchAllLists(betterSourceUrls);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
76
options.html
76
options.html
@ -1,39 +1,49 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
|
||||||
<style>
|
|
||||||
body {
|
|
||||||
margin: 40px auto;
|
|
||||||
max-width:650px;
|
|
||||||
line-height:1.6;
|
|
||||||
font-size:18px;
|
|
||||||
color:#444;
|
|
||||||
padding:0 10px
|
|
||||||
}
|
|
||||||
h1,h2,h3 {
|
|
||||||
line-height:1.2
|
|
||||||
}
|
|
||||||
|
|
||||||
input[type=url] {
|
<head>
|
||||||
width: 100%;
|
<style>
|
||||||
padding: 12px 20px;
|
body {
|
||||||
margin: 8px 0;
|
margin: 40px auto;
|
||||||
box-sizing: border-box;
|
max-width: 650px;
|
||||||
}
|
line-height: 1.6;
|
||||||
|
font-size: 18px;
|
||||||
|
color: #444;
|
||||||
|
padding: 0 10px
|
||||||
|
}
|
||||||
|
|
||||||
button {
|
h1,
|
||||||
padding: 5px 5px;
|
h2,
|
||||||
font-size: 16px;
|
h3 {
|
||||||
}
|
line-height: 1.2
|
||||||
</style>
|
}
|
||||||
</head>
|
|
||||||
<body>
|
input[type=url] {
|
||||||
<div>
|
width: 100%;
|
||||||
<h3>Choose a different source of suggestions!</h3>
|
padding: 12px 20px;
|
||||||
<!-- TODO: Fetch the current option value and set in URL field below -->
|
margin: 8px 0;
|
||||||
<input id="betterSourceText" type="url" placeholder="https://cdn.jsdelivr.net/gh/nileshtrivedi/better/defaultlist.json" />
|
box-sizing: border-box;
|
||||||
<button id="betterSourceSubmit" value="Save">Save</button>
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
padding: 5px 5px;
|
||||||
|
font-size: 16px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div>
|
||||||
|
<h3>Choose a different source of suggestions!</h3>
|
||||||
|
<!-- TODO: Fetch the current option value and set in URL field below -->
|
||||||
|
<div id="betterSourceInputContainer">
|
||||||
</div>
|
</div>
|
||||||
</body>
|
<button id="addSourceButton">Add a new source +</button>
|
||||||
<script src="options.js"></script>
|
|
||||||
|
<button id="betterSourceSubmit" value="Save">Save</button>
|
||||||
|
</div>
|
||||||
|
<p><small>To delete a source: empty the field, hit save, and refresh the page.</small></p>
|
||||||
|
</body>
|
||||||
|
<script src="options.js"></script>
|
||||||
|
|
||||||
</html>
|
</html>
|
68
options.js
68
options.js
@ -1,14 +1,60 @@
|
|||||||
let input = document.getElementById('betterSourceText');
|
let DEFAULT_LIST_URL =
|
||||||
let submit = document.getElementById('betterSourceSubmit');
|
"https://cdn.jsdelivr.net/gh/nileshtrivedi/better/defaultlist.json";
|
||||||
|
let addSourceButton = document.getElementById("addSourceButton");
|
||||||
|
let submit = document.getElementById("betterSourceSubmit");
|
||||||
|
let betterSourceInputContainer = document.getElementById(
|
||||||
|
"betterSourceInputContainer"
|
||||||
|
);
|
||||||
|
|
||||||
submit.addEventListener('click', function() {
|
function storePrefs(sourceUrls) {
|
||||||
chrome.storage.sync.set({betterSourceURL: input.value}, function() {
|
sourceUrls = sourceUrls.filter((url) => url != "");
|
||||||
console.log('Set betterSource = ' + input.value);
|
let prefs = {
|
||||||
});
|
betterSourceUrls: JSON.stringify(sourceUrls),
|
||||||
|
};
|
||||||
|
chrome.storage.sync.set(prefs, function () {
|
||||||
|
console.log("Saved", prefs);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
chrome.runtime.sendMessage({type: 'reloadList', url: input.value}, (response) => {
|
function addUrlInput(value) {
|
||||||
if (response) {
|
let newInput = document.createElement("input");
|
||||||
console.log("BETTER_ALTERNATIVES list is reloaded");
|
newInput.setAttribute("name", "url[]");
|
||||||
}
|
newInput.setAttribute("type", "url");
|
||||||
});
|
newInput.setAttribute("placeholder", "https://source.com/list.json");
|
||||||
|
if (value) {
|
||||||
|
newInput.setAttribute("value", value);
|
||||||
|
}
|
||||||
|
betterSourceInputContainer.appendChild(newInput);
|
||||||
|
}
|
||||||
|
|
||||||
|
addSourceButton.addEventListener("click", function (e) {
|
||||||
|
e.preventDefault();
|
||||||
|
addUrlInput(null);
|
||||||
|
});
|
||||||
|
|
||||||
|
submit.addEventListener("click", function () {
|
||||||
|
let $sourceUrls = document.querySelectorAll("[name='url[]']");
|
||||||
|
let sourceUrls = Array.from($sourceUrls).map((elem) => elem.value);
|
||||||
|
storePrefs(sourceUrls);
|
||||||
|
|
||||||
|
chrome.runtime.sendMessage({ type: "reloadList" }, (response) => {
|
||||||
|
if (response) {
|
||||||
|
console.log("BETTER_ALTERNATIVES list is reloaded");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
chrome.storage.sync.get(["betterSourceUrls"], (result) => {
|
||||||
|
// populate input fields from stored sources or show default source
|
||||||
|
let betterSourceUrls = [];
|
||||||
|
if (result && result.betterSourceUrls) {
|
||||||
|
betterSourceUrls = JSON.parse(result.betterSourceUrls);
|
||||||
|
}
|
||||||
|
if (betterSourceUrls.length === 0) {
|
||||||
|
betterSourceUrls = [DEFAULT_LIST_URL];
|
||||||
|
}
|
||||||
|
|
||||||
|
betterSourceUrls.map((sourceUrl) => {
|
||||||
|
addUrlInput(sourceUrl);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user