Skip to content

Commit

Permalink
Added option to prevent tabs from suspending if they are playing audio (
Browse files Browse the repository at this point in the history
  • Loading branch information
deanoemcke committed Nov 13, 2015
1 parent aa12f59 commit 0069e39
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 27 deletions.
39 changes: 24 additions & 15 deletions src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ var tgs = (function () {
notice = {},
contextMenuItems = false,
unsuspendRequestList = {},
lastTabCloseTimestamp = new Date();
lastTabCloseTimestamp = new Date(),
suspensionActiveIcon = '/img/icon19.png',
suspensionPausedIcon = '/img/icon19b.png';


//set gloabl sessionId
Expand Down Expand Up @@ -86,6 +88,11 @@ var tgs = (function () {
return dontSuspendPinned && tab.pinned;
}

function isAudibleTab(tab) {
var dontSuspendAudible = gsUtils.getOption(gsUtils.IGNORE_AUDIO);
return dontSuspendAudible && tab.audible;
}

function isExcluded(tab) {
if (tab.active) {
return true;
Expand All @@ -104,6 +111,10 @@ var tgs = (function () {
if (isPinnedTab(tab)) {
return true;
}

if (isAudibleTab(tab)) {
return true;
}
return false;
}

Expand Down Expand Up @@ -659,6 +670,7 @@ var tgs = (function () {
//suspended: a tab that is suspended
//never: suspension timer set to 'never suspend'
//formInput: a tab that has a partially completed form (and IGNORE_FORMS is true)
//audible: a tab that is playing audio (and IGNORE_AUDIO is true)
//tempWhitelist: a tab that has been manually paused
//pinned: a pinned tab (and IGNORE_PINNED is true)
//whitelisted: a tab that has been whitelisted
Expand All @@ -668,11 +680,11 @@ var tgs = (function () {
function requestTabInfo(tabId, callback) {

var info = {
windowId: '',
tabId: '',
status: 'unknown',
timerUp: '-'
};
windowId: '',
tabId: '',
status: 'unknown',
timerUp: '-'
};
tabId = tabId || globalCurrentTabId;

if (typeof(tabId) === 'undefined') {
Expand All @@ -697,7 +709,7 @@ var tgs = (function () {
callback(info);

//check if it has already been suspended
} else if (isSuspended(tab)) {
} else if (isSuspended(tab)) {
info.status = 'suspended';
callback(info);

Expand Down Expand Up @@ -745,6 +757,10 @@ var tgs = (function () {
} else if (status === 'normal' && isPinnedTab(tab)) {
status = 'pinned';

//check audible tab
} else if (status === 'normal' && isAudibleTab(tab)) {
status = 'audible';

//check never suspend
} else if (status === 'normal' && suspendTime === "0") {
status = 'never';
Expand All @@ -762,15 +778,8 @@ var tgs = (function () {

//change the icon to either active or inactive
function updateIcon(status) {
var icon = '/img/icon19.png',
dontSuspendForms = gsUtils.getOption(gsUtils.IGNORE_FORMS),
dontSuspendPinned = gsUtils.getOption(gsUtils.IGNORE_PINNED);

var icon = status !== 'normal' ? suspensionPausedIcon : suspensionActiveIcon;
lastStatus = status;

if (status !== 'normal') {
icon = '/img/icon19b.png';
}
chrome.browserAction.setIcon({path: icon});
}

Expand Down
7 changes: 7 additions & 0 deletions src/js/gsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
SUSPEND_TIME: 'gsTimeToSuspend',
IGNORE_PINNED: 'gsDontSuspendPinned',
IGNORE_FORMS: 'gsDontSuspendForms',
IGNORE_AUDIO: 'gsDontSuspendAudio',
IGNORE_CACHE: 'gsIgnoreCache',
ADD_CONTEXT: 'gsAddContextMenu',
NO_NAG: 'gsNoNag',
Expand Down Expand Up @@ -44,6 +45,7 @@
defaults[this.UNSUSPEND_ON_FOCUS] = false;
defaults[this.IGNORE_PINNED] = true;
defaults[this.IGNORE_FORMS] = true;
defaults[this.IGNORE_AUDIO] = true;
defaults[this.IGNORE_CACHE] = false;
defaults[this.ADD_CONTEXT] = true;
defaults[this.SUSPEND_TIME] = '60';
Expand Down Expand Up @@ -681,6 +683,11 @@
return curr_date + sup + ' ' + m_names[curr_month] + ' ' + curr_year;
},

getChromeVersion: function () {
var raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
return raw ? parseInt(raw[2], 10) : false;
},

generateHashCode: function (text) {
var hash = 0, i, chr, len;
if (text.length == 0) return hash;
Expand Down
37 changes: 26 additions & 11 deletions src/js/options.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*global gsUtils, chrome, invert, populateOption, setPreviewQualityVisibility, setOnlineCheckVisibility, resetTabTimers */
/*global gsUtils, chrome, invert, populateOption, setPreviewQualityVisibility, setOnlineCheckVisibility, setAudibleNoteVisibility, resetTabTimers */

(function () {

Expand All @@ -20,6 +20,7 @@
'unsuspendOnFocus': gsUtils.UNSUSPEND_ON_FOCUS,
'dontSuspendPinned': gsUtils.IGNORE_PINNED,
'dontSuspendForms': gsUtils.IGNORE_FORMS,
'dontSuspendAudio': gsUtils.IGNORE_AUDIO,
'ignoreCache': gsUtils.IGNORE_CACHE,
'addContextMenu': gsUtils.ADD_CONTEXT,
'timeToSuspend': gsUtils.SUSPEND_TIME,
Expand Down Expand Up @@ -71,6 +72,7 @@
}

setPreviewQualityVisibility(gsUtils.getOption(gsUtils.SHOW_PREVIEW));
setAudibleNoteVisibility(gsUtils.getChromeVersion() < 45 && gsUtils.getOption(gsUtils.IGNORE_AUDIO));
setAutoSuspendOptionsVisibility(gsUtils.getOption(gsUtils.SUSPEND_TIME) > 0);
}

Expand Down Expand Up @@ -99,6 +101,14 @@
}
}

function setAudibleNoteVisibility(visible) {
if (visible) {
document.getElementById('audibleOptionNote').style.display = 'block';
} else {
document.getElementById('audibleOptionNote').style.display = 'none';
}
}

function setPreviewQualityVisibility(visible) {
if (visible) {
document.getElementById('previewQualitySection').style.display = 'block';
Expand All @@ -122,12 +132,17 @@
function handleChange(element) {
return function () {
var pref = elementPrefMap[element.id],
interval;
interval,
chromeVersion;

//add specific screen element listeners
if (pref === gsUtils.SHOW_PREVIEW) {
setPreviewQualityVisibility(getOptionValue(element));

} else if (pref === gsUtils.IGNORE_AUDIO) {
chromeVersion = gsUtils.getChromeVersion();
setAudibleNoteVisibility(chromeVersion < 45 && getOptionValue(element));

} else if (pref === gsUtils.SUSPEND_TIME) {
interval = getOptionValue(element);
setAutoSuspendOptionsVisibility(interval > 0);
Expand Down Expand Up @@ -161,14 +176,14 @@
}
}

function CloseSettings() {
// Only close the window if we were opened in a new tab.
// Else, go back to the page we were on.
// This is to fix closing tabs if they were opened from the context menu.
if (document.referrer == "") {
window.close()
function closeSettings() {
//only close the window if we were opened in a new tab.
//else, go back to the page we were on.
//this is to fix closing tabs if they were opened from the context menu.
if (document.referrer === "") {
window.close();
} else {
history.back()
history.back();
}
}

Expand All @@ -195,10 +210,10 @@
for (i = 0; i < optionEls.length; i++) {
saveChange(optionEls[i]);
}
CloseSettings();
closeSettings();
};
cancelEl.onclick = function (e) {
CloseSettings();
closeSettings();
};
}
}, 50);
Expand Down
4 changes: 4 additions & 0 deletions src/js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
statusIconClass = 'fa fa-check';
message = 'removeWhitelist';

} else if (status === 'audible') {
statusDetail = 'Tab is playing audio.';
statusIconClass = 'fa fa-volume-up';

} else if (status === 'formInput') {
statusDetail = 'Tab is receiving form input. <a href="#">Unpause</a>';
statusIconClass = 'fa fa-edit';
Expand Down
7 changes: 6 additions & 1 deletion src/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ <h2>Suspend tab settings</h2>
<input type="checkbox" id="dontSuspendForms" class='option' />
<label for="dontSuspendForms" class="cbLabel">Do not suspend tabs that contain unsaved form inputs</label>
</div>
<div class="formRow autoSuspendOption">
<input type="checkbox" id="dontSuspendAudio" class='option' />
<label for="dontSuspendAudio" class="cbLabel">Do not suspend tabs that are playing audio</label>
</div>
<div id="audibleOptionNote" class="gsNote">You must have chrome version 45 or higher to use this feature.</div>
<div class="formRow autoSuspendOption">
<input type="checkbox" id="onlineCheck" class='option' />
<label for="onlineCheck" class="cbLabel">Only auto-suspend if connected to the internet</label>
Expand All @@ -63,7 +68,7 @@ <h2>Suspend tab settings</h2>
</div>
<div class="formRow">
<input type="checkbox" id="unsuspendOnFocus" class='option' />
<label for="unsuspendOnFocus" class="cbLabel">Automatically unsuspend when tab gains focus</label>
<label for="unsuspendOnFocus" class="cbLabel">Automatically unsuspend tab when it is viewed</label>
</div>
<div class="formRow">
<input type="checkbox" id="addContextMenu" class='option' />
Expand Down

0 comments on commit 0069e39

Please sign in to comment.