Skip to content

Commit

Permalink
Add popup for browser action
Browse files Browse the repository at this point in the history
  • Loading branch information
proginosko committed Nov 24, 2017
1 parent 1868d2f commit 6ef9655
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 11 deletions.
30 changes: 21 additions & 9 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,26 @@ function cancelLockdown(set) {
gOptions[`timedata${set}`][4] = 0;
}

// Open extension page (either create new tab or activate existing tab)
//
function openExtensionPage(url) {
let fullURL = browser.extension.getURL(url);

browser.tabs.query({ url: fullURL }).then(onGot, onError);

function onGot(tabs) {
if (tabs.length > 0) {
browser.tabs.update(tabs[0].id, { active: true });
} else {
browser.tabs.create({ url: fullURL });
}
}

function onError(error) {
browser.tabs.create({ url: fullURL });
}
}

// Open page blocked by delaying page
//
function openDelayedPage(id, url, set) {
Expand Down Expand Up @@ -727,18 +747,12 @@ function addSiteToSet(url, set) {

/*** EVENT HANDLERS BEGIN HERE ***/

function handleClick(tab) {
//log("handleClick: " + tab.id);

browser.runtime.openOptionsPage();
}

function handleMenuClick(info, tab) {
let id = info.menuItemId;
if (id == "options") {
browser.runtime.openOptionsPage();
} else if (id == "lockdown") {
browser.tabs.create({ url: "lockdown.html" });
openExtensionPage("lockdown.html");
} else if (id.startsWith("addSite-")) {
addSiteToSet(info.pageUrl, id.substr(8));
}
Expand Down Expand Up @@ -835,8 +849,6 @@ retrieveOptions();
browser.alarms.onAlarm.addListener(handleAlarm);
browser.alarms.create("LBNG", { periodInMinutes: TICK_TIME });

browser.browserAction.onClicked.addListener(handleClick);

if (browser.menus) {
browser.menus.onClicked.addListener(handleMenuClick);
}
Expand Down
Binary file added images/leechblock_logo_small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@
],

"browser_action": {
"browser_style": true,
"browser_style": false,
"default_icon": {
"16": "icons/leechblock16.png",
"32": "icons/leechblock32.png"
},
"default_title": "LeechBlock NG"
"default_popup": "popup.html",
"default_title": "LeechBlock"
}

}
17 changes: 17 additions & 0 deletions popup.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* LeechBlock popup CSS style */

body {
background-color: #fff;
color: #000;
text-align: center;
}

#button-container {
display: inline-block;
}

button {
display: inline;
width: 100%;
margin: 2px;
}
23 changes: 23 additions & 0 deletions popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>

<html lang="us">

<head>
<meta charset="utf-8">
<title>LeechBlock</title>
<link href="jquery-ui/jquery-ui.min.css" rel="stylesheet">
<link href="popup.css" rel="stylesheet">
<link href="icons/leechblock16.ico" rel="icon" type="image/x-icon">
<link href="icons/leechblock16.ico" rel="shortcut icon" type="image/x-icon">
</head>

<body>
<p><img src="images/leechblock_logo_small.png" alt="LeechBlock logo" title="LeechBlock"><p>
<div id="button-container" class="ui-widget">
<button id="options" type="button">Options</button>
<button id="lockdown" type="button">Lockdown</button>
</div>
<script src="popup.js"></script>
</body>

</html>
41 changes: 41 additions & 0 deletions popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Open options page
//
function openOptions() {
browser.runtime.openOptionsPage();
window.close();
}

// Open lockdown page
//
function openLockdown() {
openExtensionPage("lockdown.html");
}

// Open extension page (either create new tab or activate existing tab)
//
function openExtensionPage(url) {
let fullURL = browser.extension.getURL(url);

browser.tabs.query({ url: fullURL }).then(onGot, onError);

function onGot(tabs) {
if (tabs.length > 0) {
browser.tabs.update(tabs[0].id, { active: true });
} else {
browser.tabs.create({ url: fullURL });
}
window.close();
}

function onError(error) {
browser.tabs.create({ url: fullURL });
window.close();
}
}

document.querySelector("#options").addEventListener("click", openOptions);
document.querySelector("#lockdown").addEventListener("click", openLockdown);

0 comments on commit 6ef9655

Please sign in to comment.