Skip to content

Commit

Permalink
Add menus and lockdown option
Browse files Browse the repository at this point in the history
  • Loading branch information
proginosko committed Oct 29, 2017
1 parent 122ee42 commit adc8199
Show file tree
Hide file tree
Showing 6 changed files with 271 additions and 5 deletions.
54 changes: 52 additions & 2 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,31 @@ var focusedWindowId = -1;
function log(message) { console.log("[LBNG] " + message); }
function warn(message) { console.warn("[LBNG] " + message); }

// Creates menus
//
function createMenus() {
// Lockdown
browser.menus.create({
id: "lockdown",
type: "normal",
title: "Lockdown",
contexts: ["all", "tools_menu"]});

// Options
browser.menus.create({
id: "options",
type: "normal",
title: "Options",
contexts: ["all", "tools_menu"]});

browser.menus.onClicked.addListener(onMenuClick);

function onMenuClick(info, tab) {
let id = info.menuItemId;
browser.tabs.create({ url: `${id}.html` });
}
}

// Retrieves options from local storage
//
function retrieveOptions() {
Expand Down Expand Up @@ -525,6 +550,21 @@ function getUnblockTime(set) {
return null;
}

// Apply lockdown for specified set
//
function applyLockdown(set, endTime) {
//log("applyLockdown: " + set + " " + endTime);

let timedata = OPTIONS[`timedata${set}`];

// Apply lockdown only if it doesn't reduce any current lockdown
if (endTime > timedata[4]) {
timedata[4] = endTime;
}

OPTIONS[`timedata${set}`] = timedata;
}

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

function handleMessage(message, sender, sendResponse) {
Expand All @@ -533,11 +573,19 @@ function handleMessage(message, sender, sendResponse) {
return;
}

//log("handleMessage: " + sender.tab.id + " " + sender.url);
//log("handleMessage: " + sender.tab.id + " " + message.type);

if (message.type == "options") {
if (message.type == "close") {
// Close tab requested
browser.tabs.remove(sender.tab.id);
} else if (message.type == "options") {
// Options updated
retrieveOptions();
} else if (message.type == "lockdown") {
// Lockdown requested
applyLockdown(message.set, message.endTime);
} else if (message.type == "blocked") {
// Blocking page requested block info
let info = createBlockInfo(sender.url);
sendResponse(info);
}
Expand Down Expand Up @@ -622,6 +670,8 @@ function handleAlarm(alarm) {

/*** STARTUP CODE BEGINS HERE ***/

createMenus();

retrieveOptions();

browser.runtime.onMessage.addListener(handleMessage);
Expand Down
93 changes: 93 additions & 0 deletions lockdown.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<!DOCTYPE html>

<html lang="us">

<head>
<meta charset="utf-8">
<title>LeechBlock Lockdown</title>
<link href="jquery-ui/jquery-ui.min.css" rel="stylesheet">
<link href="options.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>

<div id="form-container">

<form id="form" style="display: none;">

<fieldset class="ui-widget ui-widget-content">
<legend>Lockdown Options</legend>
<p>
<label>Enter the duration of the lockdown:</label><br>
<div>
<input id="hours" type="text" size="2" list="hours-list">
<datalist id="hours-list">
<option value="0">
<option value="1">
<option value="2">
<option value="3">
<option value="4">
<option value="5">
<option value="6">
<option value="12">
<option value="18">
<option value="24">
</datalist>
<label for="hours">hour(s)</label>
<input id="mins" type="text" size="2" list="mins-list">
<datalist id="mins-list">
<option value="0">
<option value="5">
<option value="10">
<option value="15">
<option value="20">
<option value="25">
<option value="30">
<option value="35">
<option value="40">
<option value="45">
<option value="50">
<option value="55">
</datalist>
<label for="mins">minute(s)</label>
</div>
</p>
<hr>
<p>
<label>Select the sites to block during the lockdown:</label><br>
<div id="sites">
<p>
<input id="site1" type="checkbox"><label id="siteLabel1" for="site1">Sites specified in Block Set 1</label>
</p>
</div>
</p>
</fieldset>

<p style="text-align: center;">
<button id="activate" type="button">Activate Lockdown</button>
<button id="cancel" type="button">Cancel</button>
</p>

</form>

</div>

<div id="alerts" style="display: none;">
<div id="alertNoDuration" title="LeechBlock Lockdown">
<p>Please enter the duration of the lockdown.</p>
</div>
<div id="alertNoSets" title="LeechBlock Lockdown">
<p>Please select the sites to lockdown.</p>
</div>
</div>

<script src="jquery-ui/external/jquery/jquery.js"></script>
<script src="jquery-ui/jquery-ui.min.js"></script>
<script src="common.js"></script>
<script src="lockdown.js"></script>

</body>

</html>
110 changes: 110 additions & 0 deletions lockdown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/* 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/. */

function log(message) { console.log("[LBNG] " + message); }
function warn(message) { console.warn("[LBNG] " + message); }

// Initializes form
//
function initializeForm() {
//log("initializeForm");

browser.storage.local.get().then(onGot, onError);

function onGot(options) {
for (let set = 1; set <= NUM_SETS; set++) {
// Append custom set name to check box label (if specified)
let setName = options[`setName${set}`];
if (setName) {
document.querySelector(`#siteLabel${set}`).innerText += ` (${setName})`;
}
}
}

function onError(error) {
warn("Cannot get options: " + error);
}
}

// Handles activate button click
//
function onActivate() {
//log("onActivate");

// Get lockdown duration
let hours = document.querySelector("#hours").value;
let mins = document.querySelector("#mins").value;
let duration = hours * 3600 + mins * 60;

if (!duration) {
$("#alertNoDuration").dialog("open");
return;
}

// Calculate end time for lockdown
let endTime = Math.floor(Date.now() / 1000) + duration;

// Request lockdown for each selected set
let noneSelected = true;
for (let set = 1; set <= NUM_SETS; set++) {
let selected = document.querySelector(`#site${set}`).checked;
if (selected) {
noneSelected = false;
let message = {
type: "lockdown",
endTime: endTime,
set: set
};
// Request lockdown for this set
browser.runtime.sendMessage(message);
}
}

if (noneSelected) {
$("#alertNoSets").dialog("open");
return;
}

// Request tab close
browser.runtime.sendMessage({ type: "close" });
}

// Handles cancel button click
//
function onCancel() {
//log("onCancel");

// Request tab close
browser.runtime.sendMessage({ type: "close" });
}

// Use HTML for first check box to create other check boxes
let siteHTML = $("#sites").html();
for (let set = 2; set <= NUM_SETS; set++) {
let nextSiteHTML = siteHTML
.replace(/Block Set 1/g, `Block Set ${set}`)
.replace(/(id|for)="(\w+)1"/g, `$1="$2${set}"`);
$("#sites").append(nextSiteHTML);
}

// Set up JQuery UI widgets
$("#activate").button();
$("#activate").click(onActivate);
$("#cancel").button();
$("#cancel").click(onCancel);

let alerts = ["alertNoDuration", "alertNoSets"];
for (let alert of alerts) {
$(`#${alert}`).dialog({
autoOpen: false,
modal: true,
buttons: {
OK: function() { $(this).dialog("close"); }
}
});
}

$("#form").show();

document.addEventListener("DOMContentLoaded", initializeForm);
1 change: 1 addition & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

"permissions": [
"alarms",
"menus",
"storage",
"tabs",
"webNavigation"
Expand Down
4 changes: 4 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ <h3>Block Set 1<span id="blockSetCustomName1"></span></h3>
<input id="prevConfig1" type="checkbox">
<label for="prevConfig1">Prevent access to about:config and about:support at times when these sites are blocked</label>
</p>
<hr>
<p>
<button id="cancelLockdown1" type="button" disabled="true">Cancel Active Lockdown</button>
</p>
</fieldset>
</div>
</div>
Expand Down
14 changes: 11 additions & 3 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ function retrieveOptions() {
// Get current time in seconds
let now = Math.floor(Date.now() / 1000);

// Check whether a lockdown is currently active
for (let set = 1; set <= NUM_SETS; set++) {
let timedata = options[`timedata${set}`];
if (now < timedata[4]) {
document.querySelector(`#cancelLockdown${set}`).disabled = false;
}
}

// Check whether access to options should be prevented
for (let set = 1; set <= NUM_SETS; set++) {
if (options[`prevOpts${set}`]) {
Expand Down Expand Up @@ -234,7 +242,7 @@ function disableSetOptions(set) {
"day0", "day1", "day2", "day3", "day4", "day5", "day6",
"blockURL", "defaultPage", "delayingPage", "blankPage", "homePage",
"activeBlock", "countFocus", "delayFirst", "delaySecs",
"prevOpts", "prevAddons", "prevConfig"
"prevOpts", "prevAddons", "prevConfig", "cancelLockdown"
];
for (let item of items) {
let element = document.querySelector(`#${item}${set}`);
Expand All @@ -247,10 +255,10 @@ function disableSetOptions(set) {
// Use HTML for first block set to create other block sets
let blockSetHTML = $("#panes").html();
for (let set = 2; set <= NUM_SETS; set++) {
let newBlockSetHTML = blockSetHTML
let nextBlockSetHTML = blockSetHTML
.replace(/Block Set 1/g, `Block Set ${set}`)
.replace(/(id|for)="(\w+)1"/g, `$1="$2${set}"`);
$("#panes").append(newBlockSetHTML);
$("#panes").append(nextBlockSetHTML);
}

// Set up JQuery UI widgets
Expand Down

0 comments on commit adc8199

Please sign in to comment.