Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Volume control #60

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"message": "Enforces a 25min/5min workflow: 25 minutes of distraction-free work, followed by 5 minutes of break. Repeat as necessary.",
"description": "Description of the extension"
},

"work": {
"message": "work",
"description": "The name of the work timer"
Expand All @@ -20,7 +20,7 @@
"message": "minutes",
"description": "The name of the time interval representing 60 seconds"
},

"site_blocked_info": {
"message": "Page blocked until a break timer starts.",
"description": "Text for the blocked-site overlay, explaining that the site is blocked and when it will become unblocked"
Expand All @@ -29,7 +29,7 @@
"message": "Back to work!",
"description": "Text for the blocked-site overlay, motivating the user to get back to work"
},

"options_title": {
"message": "Strict Workflow Options",
"description": "The title of the options page"
Expand Down Expand Up @@ -86,7 +86,7 @@
"message": "Save successful",
"description": "On the options page, message to indicate that the options have been saved"
},

"timer_end_notification_header": {
"message": "Time's up!",
"description": "Bold header of pop-up notification when a timer ends"
Expand All @@ -100,5 +100,23 @@
"example": "work"
}
}
},
"contextmenu_volume_label": {
"message": "Volume: $volume$% (Click to test)",
"description": "Volume value that appears in context menu.",
"placeholders": {
"volume": {
"content": "$1",
"example": "80"
}
}
},
"contextmenu_volume_up_label": {
"message": "Volume Up",
"description": "Label that appears in context menu for \"Volume Up\"."
},
"contextmenu_volume_down_label": {
"message": "Volume Down",
"description": "Label that appears in context menu for \"Volume Down\"."
}
}
74 changes: 63 additions & 11 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function defaultPrefs() {
break: 5 * 60
},
shouldRing: true,
volume: 1,
clickRestarts: false,
whitelist: false
}
Expand All @@ -54,25 +55,25 @@ function updatePrefsFormat(prefs) {
// say, adding boolean flags with false as the default, there's no
// compatibility issue. However, in more complicated situations, we need
// to modify an old PREFS module's structure for compatibility.

if(prefs.hasOwnProperty('domainBlacklist')) {
// Upon adding the whitelist feature, the domainBlacklist property was
// renamed to siteList for clarity.

prefs.siteList = prefs.domainBlacklist;
delete prefs.domainBlacklist;
savePrefs(prefs);
console.log("Renamed PREFS.domainBlacklist to PREFS.siteList");
}

if(!prefs.hasOwnProperty('showNotifications')) {
// Upon adding the option to disable notifications, added the
// showNotifications property, which defaults to true.
prefs.showNotifications = true;
savePrefs(prefs);
console.log("Added PREFS.showNotifications");
}

return prefs;
}

Expand All @@ -91,9 +92,11 @@ function loadRingIfNecessary() {
console.log('is ring necessary?');
if(PREFS.shouldRing && !ringLoaded) {
console.log('ring is necessary');
RING.volume = PREFS.volume;
RING.onload = function () {
console.log('ring loaded');
ringLoaded = true;
RING.volume = PREFS.volume;
}
RING.load();
}
Expand Down Expand Up @@ -143,7 +146,7 @@ function Pomodoro(options) {
this.currentTimer = new Pomodoro.Timer(this, timerOptions);
this.currentTimer.start();
}

this.restart = function () {
if(this.currentTimer) {
this.currentTimer.restart();
Expand All @@ -162,7 +165,7 @@ Pomodoro.Timer = function Timer(pomodoro, options) {
options.onStart(timer);
options.onTick(timer);
}

this.restart = function() {
this.timeRemaining = options.duration;
options.onTick(timer);
Expand Down Expand Up @@ -260,7 +263,7 @@ function isLocationBlocked(location) {
return !PREFS.whitelist;
}
}

// If we're in a whitelist, an unmatched location is blocked => true
// If we're in a blacklist, an unmatched location is not blocked => false
return PREFS.whitelist;
Expand All @@ -270,7 +273,7 @@ function executeInTabIfBlocked(action, tab) {
var file = "content_scripts/" + action + ".js", location;
location = tab.url.split('://');
location = parseLocation(location[1]);

if(isLocationBlocked(location)) {
chrome.tabs.executeScript(tab.id, {file: file});
}
Expand All @@ -296,7 +299,7 @@ var notification, mainPomodoro = new Pomodoro({
path: ICONS.ACTION.PENDING[timer.pomodoro.nextMode]
});
chrome.browserAction.setBadgeText({text: ''});

if(PREFS.showNotifications) {
var nextModeName = chrome.i18n.getMessage(timer.pomodoro.nextMode);
chrome.notifications.create("", {
Expand All @@ -308,7 +311,7 @@ var notification, mainPomodoro = new Pomodoro({
iconUrl: ICONS.FULL[timer.type]
}, function() {});
}

if(PREFS.shouldRing) {
console.log("playing ring", RING);
RING.play();
Expand Down Expand Up @@ -342,7 +345,7 @@ var notification, mainPomodoro = new Pomodoro({
});

chrome.browserAction.onClicked.addListener(function (tab) {
if(mainPomodoro.running) {
if(mainPomodoro.running) {
if(PREFS.clickRestarts) {
mainPomodoro.restart();
}
Expand All @@ -364,3 +367,52 @@ chrome.notifications.onClicked.addListener(function (id) {
chrome.windows.update(window.id, {focused: true});
});
});

/*
Context menu
*/

chrome.contextMenus.create({
contexts: ["browser_action"],
"title": chrome.i18n.getMessage("contextmenu_volume_up_label"),
"onclick": volumeUp
});

chrome.contextMenus.create({
contexts: ["browser_action"],
"title": chrome.i18n.getMessage("contextmenu_volume_down_label"),
"onclick": volumeDown
});

var volumeLabel = chrome.contextMenus.create({
contexts: ["browser_action"],
"title": getVolumeStatusLabel(),
"onclick": volumeTest
});

function volumeUp() {
adjustVolume(1);
}

function volumeDown() {
adjustVolume(-1);
}

function adjustVolume(adjustBy) {
RING.volume = (RING.volume * 10 + adjustBy) / 10;
updateVolumeLabel();
PREFS.volume = RING.volume;
savePrefs(PREFS);
}

function volumeTest() {
RING.play();
}

function updateVolumeLabel() {
chrome.contextMenus.update(volumeLabel, {title: getVolumeStatusLabel() })
}

function getVolumeStatusLabel() {
return chrome.i18n.getMessage("contextmenu_volume_label", [RING.volume * 100]);
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"manifest_version": 2,
"name": "__MSG_ext_name__",
"options_page": "options.html",
"permissions": [ "notifications", "tabs", "<all_urls>" ],
"permissions": [ "notifications", "tabs", "<all_urls>", "contextMenus" ],
"version": "1.7.0",
"web_accessible_resources": [
"icons/work_full.png",
Expand Down