Skip to content

Commit

Permalink
Update timers and suspended tab state on option save and event action
Browse files Browse the repository at this point in the history
  • Loading branch information
deanoemcke committed Apr 17, 2018
1 parent ff30d77 commit edd2397
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 138 deletions.
28 changes: 4 additions & 24 deletions src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,6 @@ var tgs = (function () { // eslint-disable-line no-unused-vars
});
}

function getAllExpiredTabs(callback) {
var expiredTabs = [];
var checkTabExpiryPromises = [];
chrome.tabs.query({}, function (tabs) {
tabs.forEach(function (currentTab) {
if(gsUtils.isNormalTab(currentTab) && !gsUtils.isDiscardedTab(currentTab)) {
checkTabExpiryPromises.push(new Promise(function (resolve) {
gsMessages.sendRequestInfoToContentScript(currentTab.id, function (err, tabInfo) {
if (tabInfo && tabInfo.timerUp && (new Date(tabInfo.timerUp)) < new Date()) {
expiredTabs.push(currentTab);
}
resolve();
});
}));
}
});
Promise.all(checkTabExpiryPromises).then(function () {
callback(expiredTabs);
});
});
}

// NOTE: Stationary here means has had focus for more than FOCUS_DELAY ms
// So it may not necessarily have the tab.active flag set to true
function isCurrentStationaryTab(tab) {
Expand Down Expand Up @@ -1112,7 +1090,7 @@ var tgs = (function () { // eslint-disable-line no-unused-vars
//restart timer on all normal tabs
//NOTE: some tabs may have been prevented from suspending when computer was charging
if (!_isCharging && gsStorage.getOption(gsStorage.IGNORE_WHEN_CHARGING)) {
gsMessages.sendResetToContentScripts([gsStorage.SUSPEND_TIME]);
gsMessages.sendResetTimerToAllContentScripts();
}
};
});
Expand All @@ -1124,7 +1102,7 @@ var tgs = (function () { // eslint-disable-line no-unused-vars
//restart timer on all normal tabs
//NOTE: some tabs may have been prevented from suspending when internet was offline
if (gsStorage.getOption(gsStorage.IGNORE_WHEN_OFFLINE)) {
gsMessages.sendResetToContentScripts([gsStorage.SUSPEND_TIME]);
gsMessages.sendResetTimerToAllContentScripts();
}
setIconStatusForActiveTab();
});
Expand Down Expand Up @@ -1155,12 +1133,14 @@ var tgs = (function () { // eslint-disable-line no-unused-vars
resuspendSuspendedTab: resuspendSuspendedTab,
getActiveTabStatus: getActiveTabStatus,
getDebugInfo: getDebugInfo,
calculateTabStatus: calculateTabStatus,
isCharging: isCharging,
isCurrentStationaryTab: isCurrentStationaryTab,
isCurrentFocusedTab: isCurrentFocusedTab,

initialiseUnsuspendedTab: initialiseUnsuspendedTab,
initialiseSuspendedTab: initialiseSuspendedTab,
unsuspendTab: unsuspendTab,
unsuspendHighlightedTab: unsuspendHighlightedTab,
unwhitelistHighlightedTab: unwhitelistHighlightedTab,
undoTemporarilyWhitelistHighlightedTab: undoTemporarilyWhitelistHighlightedTab,
Expand Down
59 changes: 34 additions & 25 deletions src/js/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
'use strict';

var isInitialised = false,
inputState = false,
isFormListenerInitialised = false,
isReceivingFormInput = false,
isIgnoreForms = false,
tempWhitelist = false,
timerJob,
suspendDateTime = false;
Expand All @@ -24,25 +26,30 @@

return setTimeout(function () {
//request suspension
if (!inputState && !tempWhitelist) {
if (!isReceivingFormInput && !tempWhitelist) {
chrome.runtime.sendMessage({ action: 'suspendTab' });
}
suspendDateTime = false;
}, timeToSuspend);
}

function formInputListener(event) {
if (!inputState && !tempWhitelist) {
if (event.keyCode >= 48 && event.keyCode <= 90 && event.target.tagName) {
if (event.target.tagName.toUpperCase() === 'INPUT' ||
function initFormInputListener() {
if (isFormListenerInitialised) {
return;
}
window.addEventListener('keydown', function (event) {
if (!isReceivingFormInput && !tempWhitelist) {
if (event.keyCode >= 48 && event.keyCode <= 90 && event.target.tagName) {
if (event.target.tagName.toUpperCase() === 'INPUT' ||
event.target.tagName.toUpperCase() === 'TEXTAREA' ||
event.target.tagName.toUpperCase() === 'FORM' ||
event.target.isContentEditable === true) {
inputState = true;
chrome.runtime.sendMessage(buildReportTabStatePayload());
isReceivingFormInput = true;
chrome.runtime.sendMessage(buildReportTabStatePayload());
}
}
}
}
});
isFormListenerInitialised = true;
}

//listen for background events
Expand All @@ -58,25 +65,15 @@
isInitialised = true;
}

if (request.hasOwnProperty('ignoreForms')) {
window.removeEventListener('keydown', formInputListener);
if (request.ignoreForms) {
window.addEventListener('keydown', formInputListener);
}
inputState = inputState && request.ignoreForms;
}
if (request.hasOwnProperty('tempWhitelist')) {
if (inputState && !request.tempWhitelist) {
inputState = false;
}
tempWhitelist = request.tempWhitelist;
}
if (request.hasOwnProperty('scrollPos')) {
if (request.scrollPos !== '' && request.scrollPos !== '0') {
document.body.scrollTop = request.scrollPos;
document.documentElement.scrollTop = request.scrollPos;
}
}
if (request.hasOwnProperty('ignoredFormsSuspendTime') && isReceivingFormInput) {
request.suspendTime = request.ignoredFormsSuspendTime;
}
if (request.hasOwnProperty('suspendTime')) {
clearTimeout(timerJob);
var suspendTime = Number(request.suspendTime);
Expand All @@ -86,15 +83,27 @@
suspendDateTime = false;
}
}
if (request.hasOwnProperty('ignoreForms')) {
isIgnoreForms = request.ignoreForms;
if (isIgnoreForms) {
initFormInputListener();
}
}
if (request.hasOwnProperty('tempWhitelist')) {
if (isReceivingFormInput && !request.tempWhitelist) {
isReceivingFormInput = false;
}
tempWhitelist = request.tempWhitelist;
}
sendResponse(buildReportTabStatePayload());
return false;
});

function buildReportTabStatePayload(state) {
function buildReportTabStatePayload() {
return {
action: 'reportTabState',
isInitialised: isInitialised,
status: state || (inputState ? 'formInput' : (tempWhitelist ? 'tempWhitelist' : 'normal')),
status: (isIgnoreForms && isReceivingFormInput) ? 'formInput' : (tempWhitelist ? 'tempWhitelist' : 'normal'),
scrollPos: document.body.scrollTop || document.documentElement.scrollTop || 0,
timerUp: suspendDateTime ? suspendDateTime + '' : false,
};
Expand Down
66 changes: 32 additions & 34 deletions src/js/gsMessages.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,32 @@
/*global gsUtils, gsSession, gsStorage */
/*global tgs, gsUtils, gsSession, gsStorage */
var gsMessages = { // eslint-disable-line no-unused-vars

INFO: 'info',
WARNING: 'warning',
ERROR: 'error',

sendInitTabToContentScript(tabId, ignoreForms, tempWhitelist, scrollPos, suspendTime, callback) {
var props = {
var payload = {
action: 'initialiseContentScript',
ignoreForms: ignoreForms,
tempWhitelist: tempWhitelist,
};
if (scrollPos) {
props.scrollPos = scrollPos;
payload.scrollPos = scrollPos;
}
if (suspendTime !== null && !isNaN(Number(suspendTime))) {
props.suspendTime = suspendTime;
payload.suspendTime = suspendTime;
}
this.sendMessageToContentScript(tabId, props, this.ERROR, callback);
this.sendMessageToContentScript(tabId, payload, this.ERROR, callback);
},

sendResetToAllContentScripts: function (preferencesToUpdate) {
sendResetTimerToAllContentScripts: function () {
var self = this;
var suspendTime;
var ignoreForms;
var activeTabSuspendTime;
if (preferencesToUpdate.indexOf(gsStorage.SUSPEND_TIME) > -1) {
suspendTime = gsStorage.getOption(gsStorage.SUSPEND_TIME);
}
if (preferencesToUpdate.indexOf(gsStorage.IGNORE_FORMS) > -1) {
ignoreForms = gsStorage.getOption(gsStorage.IGNORE_FORMS);
}
if (preferencesToUpdate.indexOf(gsStorage.IGNORE_ACTIVE_TABS) > -1) {
const ignoreActiveTabs = gsStorage.getOption(gsStorage.IGNORE_ACTIVE_TABS);
activeTabSuspendTime = ignoreActiveTabs ? '0' : gsStorage.getOption(gsStorage.SUSPEND_TIME);
}
var suspendTime = gsStorage.getOption(gsStorage.SUSPEND_TIME);

chrome.tabs.query({}, function (tabs) {
tabs.forEach(function (currentTab) {
if (gsUtils.isSpecialTab(currentTab) || gsUtils.isSuspendedTab(currentTab) || gsUtils.isDiscardedTab(currentTab)) {
return true;
}

let tabPayload = {};
if (typeof ignoreForms !== 'undefined') {
tabPayload.ignoreForms = ignoreForms;
}
if (typeof suspendTime !== 'undefined') {
tabPayload.suspendTime = suspendTime;
}
if (typeof activeTabSuspendTime !== 'undefined' && gsUtils.isProtectedActiveTab(currentTab, true)) {
tabPayload.suspendTime = activeTabSuspendTime;
}
self.sendMessageToContentScript(currentTab.id, tabPayload, this.WARNING, function (err) {
self.sendMessageToContentScript(currentTab.id, { suspendTime: suspendTime }, this.WARNING, function (err) {
if (err) {
gsUtils.log(currentTab.id, 'Failed to resetContentScript. Tab is probably loading?', err);
}
Expand All @@ -60,6 +35,29 @@ var gsMessages = { // eslint-disable-line no-unused-vars
});
},

sendUpdateToContentScriptOfTab: function (tab, updateSuspendTime, updateIgnoreForms) {
var self = this;
var suspendTime = gsStorage.getOption(gsStorage.SUSPEND_TIME);
var ignoreForms = gsStorage.getOption(gsStorage.IGNORE_FORMS);

if (gsUtils.isSpecialTab(tab) || gsUtils.isSuspendedTab(tab) || gsUtils.isDiscardedTab(tab)) {
return;
}

let tabPayload = {};
let tabSuspendTime = gsUtils.isProtectedActiveTab(tab) ? '0' : suspendTime;
if (updateSuspendTime) {
tabPayload.suspendTime = tabSuspendTime;
}
if (updateIgnoreForms) {
tabPayload.ignoreForms = ignoreForms;
if (!ignoreForms) {
tabPayload.ignoredFormsSuspendTime = tabSuspendTime;
}
}
self.sendMessageToContentScript(tab.id, tabPayload, this.WARNING);
},

sendClearTimerToContentScript: function (tabId, callback) {
this.sendMessageToContentScript(tabId, {
suspendTime: '0',
Expand Down
6 changes: 5 additions & 1 deletion src/js/gsStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,22 @@ var gsStorage = {
if (shouldSync) {
var localSettings = self.getSettings();
var changedSettingKeys = [];
var oldValueBySettingKey = {};
var newValueBySettingKey = {};
Object.keys(remoteSettings).forEach(function (key) {
var remoteSetting = remoteSettings[key];
if (localSettings[key] !== remoteSetting.newValue) {
gsUtils.log('gsStorage', 'Changed value from sync', key, remoteSetting.newValue);
changedSettingKeys.push(key);
oldValueBySettingKey[key] = localSettings[key];
newValueBySettingKey[key] = remoteSetting.newValue;
localSettings[key] = remoteSetting.newValue;
}
});

if (changedSettingKeys.length > 0) {
self.saveSettings(localSettings);
gsUtils.performPostSaveUpdates(changedSettingKeys);
gsUtils.performPostSaveUpdates(changedSettingKeys, oldValueBySettingKey, newValueBySettingKey);
}
}
});
Expand Down
Loading

0 comments on commit edd2397

Please sign in to comment.