Skip to content
This repository has been archived by the owner on Aug 27, 2019. It is now read-only.

Commit

Permalink
Implement options
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Apr 13, 2017
1 parent b5eda88 commit c92ef9c
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 31 deletions.
13 changes: 8 additions & 5 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ function registerPage(message, resolve, privileged) {
numeric: true
})
});
browser.runtime.sendMessage({
type: "pagesChanged"
}).catch((error) => {
console.error(error);
})
}
resolve(isNew);
return;
Expand All @@ -86,13 +91,11 @@ browser.runtime.onMessage.addListener((message, sender, resolve) => {
switch (messageType) {
case "registerPage": {
registerPage(message, resolve, true);
break;
return;
} case "getPages": {
resolve({pages: ABOUT_PAGES.slice()});
return;
} default: {
throw new Error("Invalid message type: " + messageType);
}
} default: {}
}
});

Expand All @@ -103,7 +106,7 @@ browser.runtime.onMessageExternal.addListener((message, sender, resolve) => {
registerPage(message, resolve, false);
break;
} default: {
throw new Error("Invalid message type: " + messageType);
throw "Invalid message type: " + messageType;
}
}
});
12 changes: 8 additions & 4 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

"manifest_version": 2,
"name": "Firefox about:about Button",
"version": "0.1.0b1",
"version": "0.1.1",
"author": "ExE Boss",
"description": "A Firefox exclusive WebExtension to quickly open about:* pages",

Expand All @@ -30,8 +30,14 @@
"background": {
"scripts": ["background.js"]
},
"options_ui": {
"page": "options/options.html",
"chrome_style": true
},
"browser_action": {
"browser_style": true,
"default_title": "about:about",
"default_popup": "popup/popup.html",
"default_icon": {
"16": "icons/16/cog.png",
"24": "icons/24/cog.png",
Expand All @@ -42,8 +48,6 @@
"96": "icons/96/cog.png",
"128":"icons/128/cog.png",
"256":"icons/256/cog.png"
},
"default_title": "about:about",
"default_popup": "popup/popup.html"
}
}
}
2 changes: 1 addition & 1 deletion src/options/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</head>
<body>
<div>
<input id="showDisabledButtons" type="checkbox" checked>Show disabled buttons
<input id="showDisabledButtons" type="checkbox">Show greyed-out buttons
</div>
</body>
</html>
45 changes: 40 additions & 5 deletions src/options/options.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,48 @@
/* global browser */

function restore_options() {
/** @type HTMLInputElement */
let element_showDisabledButtons;

function reload() {
browser.storage.local.get({
showDisabledButtons: true
showDisabledButtons: false
}).then((settings) => {
document.getElementById("showDisabledButtons").checked = settings.showDisabledButtons;
element_showDisabledButtons.checked = settings.showDisabledButtons;
}).catch((error) => {
element_showDisabledButtons.checked = false;
});
}

function saveOptions() {
browser.storage.local.set({
showDisabledButtons: element_showDisabledButtons.checked
}).catch((error) => {
document.getElementById("showDisabledButtons").checked = true;
console.error("Could not save options");
console.error(error);
});
}

document.addEventListener('DOMContentLoaded', () => {restore_options()});
document.addEventListener("DOMContentLoaded", function () {
element_showDisabledButtons = document.getElementById("showDisabledButtons");
for(let inputElement of document.getElementsByTagName("input")) {
let save = inputElement.hasAttribute("data-save") ? Boolean(inputElement.getAttribute("data-save")) : true;
if (!save) continue;
inputElement.addEventListener("input", () => {saveOptions();});
}
for(let inputElement of document.getElementsByTagName("button")) {
let save = inputElement.hasAttribute("data-save") ? Boolean(inputElement.getAttribute("data-save")) : true;
if (!save) continue;
inputElement.addEventListener("click", () => {saveOptions();});
}
browser.storage.onChanged.addListener((changes, areaName) => {
switch (areaName) {
case "local": {
if (changes.showDisabledButtons !== undefined) {
reload();
}
break;
}
}
});
reload();
});
27 changes: 26 additions & 1 deletion src/popup/popup.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ body {
button {
width: 100% !important;
text-align: left !important;
white-space: nowrap;
white-space: nowrap !important;
}

#showDisabledButtons {
text-align: center !important;
height: calc(100% + 8px) !important;
overflow-x: visible !important;
white-space: pre-line !important;
}

button:disabled {
Expand Down Expand Up @@ -39,3 +46,21 @@ a {
.icon.missing {
margin-right: 24px !important;
}

#header {
margin: 4px !important;
padding-bottom: 4px !important;
}

#footer {
margin-top: 0px !important;
margin-left: 4px !important;
margin-right: 4px !important;
margin-bottom: 4px !important;
}

#status {
margin: 2px !important;
margin-bottom: 4px !important;
white-space: normal !important;
}
7 changes: 5 additions & 2 deletions src/popup/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
<script src="popup.js"></script>
</head>
<body>
<div id="header"></div>
<!--<div id="header"></div>-->
<main id="main">
<table id="main-table"></table>
</main>
<div id="footer"></div>
<div id="footer">
<div id="status"></div>
<button id="open-options"><img class="icon" width="16px" src="/icons/256/cog.png">Open extension options</button>
</div>
</body>
</html>
42 changes: 29 additions & 13 deletions src/popup/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,38 @@ document.addEventListener('DOMContentLoaded', () => {
}
}
});
browser.runtime.onMessage.addListener((message, sender, resolve) => {
let messageType = String(message.type);
switch (messageType) {
case "pagesChanged": {
reload();
}
}
});
document.getElementById("open-options").addEventListener("click", () => {
browser.runtime.openOptionsPage();
});
reload();
});

let use_addons_shim = true;

async function reload() {
let footer = document.getElementById("footer");
let status = document.getElementById("status");
let table = document.getElementById("main-table");

table.innerHTML = "";
footer.innerHTML = "";
table.textContent = "";
status.textContent = "";

browser.runtime.sendMessage({
type: "getPages"
}).then(async (response) => {
response.showDisabledButtons = await browser.storage.local.get({
showDisabledButtons: true
showDisabledButtons: false
}).then((settings) => {
return settings.showDisabledButtons;
}).catch((error) => {
console.error(error);
console.warn(error);
return true;
});
return response;
Expand All @@ -40,35 +53,38 @@ async function reload() {
if (showDisabledButtons === undefined)
showDisabledButtons = true;
if (!showDisabledButtons) {
footer.appendChild(document.createTextNode("Disabled buttons have been hidden"));
status.appendChild(document.createTextNode("Greyed-out buttons have been hidden"));
}
pages.forEach((page) => {
if (!page[2] && (page[0] !== "about:addons" || (page[0] === "about:addons" && !use_addons_shim)) && !showDisabledButtons) return;

let tr = document.createElement("tr");
let td = document.createElement("td");

let button = document.createElement("button");
let img = generateImg(page[1]);
button.setAttribute("type", button);
if (!page[2])
if (!page[2] && (page[0] !== "about:addons" || (page[0] === "about:addons" && !use_addons_shim)))
button.setAttribute("disabled", true);
button.appendChild(img);
button.appendChild(document.createTextNode(page[0]));
button.addEventListener("click", (evt) => {
if (page[2]) {
browser.tabs.create({url: page[0]});
} else if (page[0] === "about:addons" && use_addons_shim) {
browser.runtime.openOptionsPage();
} else {
browser.tabs.create({url: "/redirect/redirect.html?dest=" + page[0]});
}
});

if (page[2] || (!page[2] && showDisabledButtons)) {
td.appendChild(button);
tr.appendChild(td);
table.appendChild(tr);
}
td.appendChild(button);
tr.appendChild(td);
table.appendChild(tr);
});
}).catch(async (error) => {
console.error(error);
console.warn(error);
status.appendChild(document.createTextNode(error));
});
}

Expand Down

0 comments on commit c92ef9c

Please sign in to comment.