From 3c158eacbbfe98c871a0ee6ce01ffccfc3b19dd0 Mon Sep 17 00:00:00 2001 From: Evgeniy Dmitriev Date: Fri, 28 Oct 2016 14:36:26 +0300 Subject: [PATCH 1/3] Volume control context menu --- background.js | 66 ++++++++++++++++++++++++++++++++++++++++++--------- manifest.json | 2 +- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/background.js b/background.js index 75062fe..4d35397 100644 --- a/background.js +++ b/background.js @@ -54,17 +54,17 @@ 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. @@ -72,7 +72,7 @@ function updatePrefsFormat(prefs) { savePrefs(prefs); console.log("Added PREFS.showNotifications"); } - + return prefs; } @@ -143,7 +143,7 @@ function Pomodoro(options) { this.currentTimer = new Pomodoro.Timer(this, timerOptions); this.currentTimer.start(); } - + this.restart = function () { if(this.currentTimer) { this.currentTimer.restart(); @@ -162,7 +162,7 @@ Pomodoro.Timer = function Timer(pomodoro, options) { options.onStart(timer); options.onTick(timer); } - + this.restart = function() { this.timeRemaining = options.duration; options.onTick(timer); @@ -260,7 +260,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; @@ -270,7 +270,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}); } @@ -296,7 +296,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("", { @@ -308,7 +308,7 @@ var notification, mainPomodoro = new Pomodoro({ iconUrl: ICONS.FULL[timer.type] }, function() {}); } - + if(PREFS.shouldRing) { console.log("playing ring", RING); RING.play(); @@ -342,7 +342,7 @@ var notification, mainPomodoro = new Pomodoro({ }); chrome.browserAction.onClicked.addListener(function (tab) { - if(mainPomodoro.running) { + if(mainPomodoro.running) { if(PREFS.clickRestarts) { mainPomodoro.restart(); } @@ -364,3 +364,47 @@ chrome.notifications.onClicked.addListener(function (id) { chrome.windows.update(window.id, {focused: true}); }); }); + +/* + Context menu + */ + +chrome.contextMenus.create({ + contexts: ["browser_action"], + "title": "Volume Up", + "onclick": volumeUp +}); + +chrome.contextMenus.create({ + contexts: ["browser_action"], + "title": "Volume Down", + "onclick": volumeDown +}); + +var volumeLabel = chrome.contextMenus.create({ + contexts: ["browser_action"], + "title": getVolumeStatusLabel(), + "onclick": volumeTest +}); + +function volumeUp() { + RING.volume = (RING.volume * 10 + 1) / 10; + updateVolumeLabel(); +} + +function volumeDown() { + RING.volume = (RING.volume * 10 - 1) / 10; + updateVolumeLabel(); +} + +function volumeTest() { + RING.play(); +} + +function updateVolumeLabel() { + chrome.contextMenus.update(volumeLabel, {title: getVolumeStatusLabel() }) +} + +function getVolumeStatusLabel() { + return "Volume: " + (RING.volume * 100) + "% (Click to test)"; +} diff --git a/manifest.json b/manifest.json index a14614e..4fee43d 100644 --- a/manifest.json +++ b/manifest.json @@ -15,7 +15,7 @@ "manifest_version": 2, "name": "__MSG_ext_name__", "options_page": "options.html", - "permissions": [ "notifications", "tabs", "" ], + "permissions": [ "notifications", "tabs", "", "contextMenus" ], "version": "1.7.0", "web_accessible_resources": [ "icons/work_full.png", From 5d37bec34ba100f59cf237f178f2f3d542308107 Mon Sep 17 00:00:00 2001 From: Evgeniy Dmitriev Date: Fri, 28 Oct 2016 14:53:09 +0300 Subject: [PATCH 2/3] volume persist --- background.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/background.js b/background.js index 4d35397..0a59cc3 100644 --- a/background.js +++ b/background.js @@ -36,6 +36,7 @@ function defaultPrefs() { break: 5 * 60 }, shouldRing: true, + volume: 1, clickRestarts: false, whitelist: false } @@ -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(); } @@ -388,13 +391,18 @@ var volumeLabel = chrome.contextMenus.create({ }); function volumeUp() { - RING.volume = (RING.volume * 10 + 1) / 10; - updateVolumeLabel(); + adjustVolume(1); } function volumeDown() { - RING.volume = (RING.volume * 10 - 1) / 10; + adjustVolume(-1); +} + +function adjustVolume(adjustBy) { + RING.volume = (RING.volume * 10 + adjustBy) / 10; updateVolumeLabel(); + PREFS.volume = RING.volume; + savePrefs(PREFS); } function volumeTest() { From 72b50616e0ec9fdfa413a717f417997bbc7bf2e4 Mon Sep 17 00:00:00 2001 From: Evgeniy Dmitriev Date: Fri, 28 Oct 2016 15:06:09 +0300 Subject: [PATCH 3/3] localization "EN" --- _locales/en/messages.json | 26 ++++++++++++++++++++++---- background.js | 6 +++--- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/_locales/en/messages.json b/_locales/en/messages.json index d244cdd..f5c8ee4 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -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" @@ -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" @@ -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" @@ -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" @@ -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\"." } } diff --git a/background.js b/background.js index 0a59cc3..641075f 100644 --- a/background.js +++ b/background.js @@ -374,13 +374,13 @@ chrome.notifications.onClicked.addListener(function (id) { chrome.contextMenus.create({ contexts: ["browser_action"], - "title": "Volume Up", + "title": chrome.i18n.getMessage("contextmenu_volume_up_label"), "onclick": volumeUp }); chrome.contextMenus.create({ contexts: ["browser_action"], - "title": "Volume Down", + "title": chrome.i18n.getMessage("contextmenu_volume_down_label"), "onclick": volumeDown }); @@ -414,5 +414,5 @@ function updateVolumeLabel() { } function getVolumeStatusLabel() { - return "Volume: " + (RING.volume * 100) + "% (Click to test)"; + return chrome.i18n.getMessage("contextmenu_volume_label", [RING.volume * 100]); }