diff --git a/src/js/background.js b/src/js/background.js index e0c71c0d..9b761cc9 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -32,9 +32,9 @@ var tgs = (function () { sessionId = gsUtils.generateSessionId(); if (debug) console.log('sessionId: ' + sessionId); - function savePreview(tab, previewUrl, position) { + function savePreview(tab, previewUrl) { if (previewUrl) { - gsUtils.addPreviewImage(tab.url, previewUrl, position); + gsUtils.addPreviewImage(tab.url, previewUrl); } } @@ -97,17 +97,19 @@ var tgs = (function () { return dontSuspendAudible && tab.audible; } - function confirmTabSuspension(tab) { + //ask the tab to suspend itself + function confirmTabSuspension(tab, tabInfo) { - //ask the tab to suspend itself + var scrollPos = tabInfo.scrollPos || '0'; saveSuspendData(tab, {}, function() { //if we need to save a preview image if (gsUtils.getOption(gsUtils.SCREEN_CAPTURE) !== '0') { - chrome.tabs.executeScript(tab.id, { file: 'js/html2canvas.min.js' }, function () { + chrome.tabs.executeScript(tab.id, { file: 'js/html2canvas.min.js' }, function (result) { + // console.log(result); sendMessageToTab(tab.id, { action: 'generatePreview', - suspendedUrl: gsUtils.generateSuspendedUrl(tab), + suspendedUrl: gsUtils.generateSuspendedUrl(tab.url, tab.title, scrollPos), screenCapture: gsUtils.getOption(gsUtils.SCREEN_CAPTURE) }); }); @@ -115,7 +117,7 @@ var tgs = (function () { } else { sendMessageToTab(tab.id, { action: 'confirmTabSuspend', - suspendedUrl: gsUtils.generateSuspendedUrl(tab) + suspendedUrl: gsUtils.generateSuspendedUrl(tab.url, tab.title, scrollPos) }); } }); @@ -149,16 +151,14 @@ var tgs = (function () { } } - //finally, if forceLevel is 2 or greater, do an async call to ask tab for some additional internal information - if (forceLevel >= 2) { - requestTabInfoFromContentScript(tab, function(tabInfo) { - if (tabInfo && tabInfo.status !== 'formInput' && tabInfo.status !== 'tempWhitelist') { - confirmTabSuspension(tab); - } - }); - } else { - confirmTabSuspension(tab); - } + requestTabInfoFromContentScript(tab, function(tabInfo) { + tabInfo = tabInfo || {}; + if (forceLevel >= 2 && + (tabInfo.status === 'formInput' || tabInfo.status === 'tempWhitelist')) { + return; + } + confirmTabSuspension(tab, tabInfo); + }); } function whitelistHighlightedTab() { @@ -213,11 +213,7 @@ var tgs = (function () { }); } - var fakeTabProperties = { - url: linkedUrl, - title: linkedUrl - }, - suspendedUrl = gsUtils.generateSuspendedUrl(fakeTabProperties), + var suspendedUrl = gsUtils.generateSuspendedUrl(linkedUrl, linkedUrl), index = parentTab.index + 1, tabPropertyOverrides = { url: linkedUrl, @@ -365,9 +361,12 @@ var tgs = (function () { function unsuspendTab(tab) { var url = gsUtils.getSuspendedUrl(tab.url), + scrollPosition = gsUtils.getSuspendedScrollPosition(tab.url), views, result; + scrollPosByTabId[tab.id] = scrollPosition || scrollPosByTabId[tab.id]; + //bit of a hack here as using the chrome.tabs.update method will not allow //me to 'replace' the url - leaving a suspended tab in the history views = chrome.extension.getViews({type: 'tab', "windowId": tab.windowId}); @@ -741,17 +740,7 @@ var tgs = (function () { function requestTabInfoFromContentScript(tab, callback) { - - sendMessageToTab(tab.id, {action: 'requestInfo'}, function (response) { - if (response) { - var tabInfo = {}; - tabInfo.status = response.status; - tabInfo.timerUp = response.timerUp; - callback(tabInfo); - } else { - callback(false); - } - }); + sendMessageToTab(tab.id, {action: 'requestInfo'}, callback); } function processActiveTabStatus(tab, status) { @@ -913,15 +902,13 @@ var tgs = (function () { switch (request.action) { case 'initTab': - var tabScrollPos = scrollPosByTabId[sender.tab.id]; - delete scrollPosByTabId[sender.tab.id]; sendResponse({ dontSuspendForms: gsUtils.getOption(gsUtils.IGNORE_FORMS), suspendTime: gsUtils.getOption(gsUtils.SUSPEND_TIME), screenCapture: gsUtils.getOption(gsUtils.SCREEN_CAPTURE), - tabId: sender.tab.id, - scrollPos: tabScrollPos + scrollPos: scrollPosByTabId[sender.tab.id] || '0' }); + delete scrollPosByTabId[sender.tab.id]; break; case 'reportTabState': @@ -930,10 +917,6 @@ var tgs = (function () { var status = processActiveTabStatus(sender.tab, request.status); updateIcon(status); } - // If tab is reported being suspended, save the tab's reported scroll position - if (request.status === 'suspended' && request.scrollPos) { - scrollPosByTabId[sender.tab.id] = request.scrollPos; - } break; case 'suspendTab': @@ -951,7 +934,7 @@ var tgs = (function () { break; case 'savePreviewData': - savePreview(sender.tab, request.previewUrl, request.position); + savePreview(sender.tab, request.previewUrl); if (debug && sender.tab) { if (request.errorMsg) { console.log('Error from content script from tabId ' + sender.tab.id + ': ' + request.errorMsg); @@ -1139,12 +1122,12 @@ var tgs = (function () { isSpecialTab: isSpecialTab, saveSuspendData: saveSuspendData, sessionId: sessionId, - scrollPosByTabId: scrollPosByTabId, runStartupChecks: runStartupChecks, resetContentScripts: resetContentScripts, requestNotice: requestNotice, buildContextMenu: buildContextMenu, - resuspendAllSuspendedTabs: resuspendAllSuspendedTabs + resuspendAllSuspendedTabs: resuspendAllSuspendedTabs, + scrollPosByTabId: scrollPosByTabId }; }()); diff --git a/src/js/contentscript.js b/src/js/contentscript.js index 409a811d..801d6417 100644 --- a/src/js/contentscript.js +++ b/src/js/contentscript.js @@ -10,8 +10,7 @@ (function () { 'use strict'; - var tabId, - readyStateCheckInterval, + var readyStateCheckInterval, inputState = false, tempWhitelist = false, timerJob, @@ -24,15 +23,14 @@ function init() { //do startup jobs - reportState(false); - requestPreferences(function(response) { + var tabState = buildTabStateObject(); + chrome.runtime.sendMessage({ action: 'initTab' }, function (response) { //set timer job if (response && response.suspendTime > 0) { var suspendTime = response.suspendTime * (1000*60); timerJob = setTimerJob(suspendTime); - } //add form input listener @@ -40,17 +38,12 @@ window.addEventListener('keydown', formInputListener); } - if (response && response.tabId) { - - //set tabId - tabId = response.tabId; - - //handle auto-scrolling - if (response.scrollPos && response.scrollPos !== "") { - document.body.scrollTop = response.scrollPos; - } + //handle auto-scrolling + if (response && response.scrollPos && response.scrollPos !== "" && response.scrollPos !== "0") { + document.body.scrollTop = response.scrollPos; } }); + chrome.runtime.sendMessage(tabState); } function calculateState() { @@ -58,21 +51,19 @@ return status; } - function reportState(state, scrollPos) { - var message = { + function buildTabStateObject(state) { + return { action: 'reportTabState', - status: state || calculateState() + status: state || calculateState(), + scrollPos: document.body.scrollTop, + timerUp: suspendDateTime ? suspendDateTime + '' : '-' }; - if (scrollPos) { - message.scrollPos = scrollPos; - } - chrome.runtime.sendMessage(message); } - function suspendTab(suspendedUrl, scrollPosition) { + function suspendTab(suspendedUrl) { - scrollPosition = scrollPosition || document.body.scrollTop; - reportState('suspended', scrollPosition); + var tabState = buildTabStateObject('suspended'); + chrome.runtime.sendMessage(tabState); if (suspendedUrl.indexOf('suspended.html') > 0) { window.location.replace(suspendedUrl); @@ -96,8 +87,6 @@ timer = new Date(), height = 0; - var position = document.body.scrollTop; - //safety check here. don't try to use html2canvas if the page has more than 10000 elements if (elementCount < 10000) { @@ -136,10 +125,9 @@ chrome.runtime.sendMessage({ action: 'savePreviewData', previewUrl: dataUrl, - position: position, timerMsg: timer }, function () { - suspendTab(suspendedUrl, position); + suspendTab(suspendedUrl); }); } } @@ -174,30 +162,23 @@ } function formInputListener(event) { - console.log('input!'); if (!inputState && !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') { inputState = true; - reportState(false); + var tabState = buildTabStateObject(); + chrome.runtime.sendMessage(tabState); } } } } - function requestPreferences(callback) { - chrome.runtime.sendMessage({ action: 'initTab' }, function (response) { - callback(response); - }); - } - //listen for background events chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { var response = {}, - status, - suspendDate; + status; //console.dir('received contentscript.js message:' + request.action + ' [' + Date.now() + ']'); @@ -224,11 +205,8 @@ //listen for status request case 'requestInfo': - status = calculateState(); - var suspendDateString = suspendDateTime ? suspendDateTime + '' : '-'; //console.log(suspendDateString); - response = { status: status, timerUp: suspendDateString }; - sendResponse(response); + sendResponse(buildTabStateObject()); break; //cancel suspension timer job @@ -242,8 +220,6 @@ status = inputState ? 'formInput' : (tempWhitelist ? 'tempWhitelist' : 'normal'); response = {status: status}; tempWhitelist = true; - reportState(false); - sendResponse(response); break; //listen for request to undo temporary whitelisting @@ -251,8 +227,6 @@ inputState = false; tempWhitelist = false; response = {status: 'normal'}; - reportState(false); - sendResponse(response); break; //listen for preview request diff --git a/src/js/gsUtils.js b/src/js/gsUtils.js index a096e75a..8d734c6c 100644 --- a/src/js/gsUtils.js +++ b/src/js/gsUtils.js @@ -309,7 +309,7 @@ }); }, - addPreviewImage: function (tabUrl, previewUrl, position) { + addPreviewImage: function (tabUrl, previewUrl) { var self = this, server; this.getDb().then(function (s) { @@ -325,7 +325,7 @@ return Promise.resolve(); } }).then(function() { - server.add(self.DB_PREVIEWS, {url: tabUrl, img: previewUrl, pos: position}); + server.add(self.DB_PREVIEWS, {url: tabUrl, img: previewUrl}); }); }, @@ -631,11 +631,11 @@ return Math.floor(Math.random() * 1000000) + ""; }, - generateSuspendedUrl: function (tabProperties) { + generateSuspendedUrl: function (url, title, scrollPos) { var args = '#' + - 'ttl=' + encodeURIComponent(tabProperties.title) + '&' + - // 'fav=' + encodeURIComponent(tab.favIconUrl) + '&' + - 'uri=' + (tabProperties.url); + 'ttl=' + encodeURIComponent(title) + '&' + + 'pos=' + (scrollPos || '0') + '&' + + 'uri=' + (url); return chrome.extension.getURL('suspended.html' + args); }, @@ -672,6 +672,9 @@ getSuspendedTitle: function(urlStr) { return decodeURIComponent(this.getHashVariable('ttl', urlStr) || ''); }, + getSuspendedScrollPosition: function(urlStr) { + return decodeURIComponent(this.getHashVariable('pos', urlStr) || ''); + }, getSuspendedUrl: function (urlStr) { return this.getHashVariable('uri', urlStr); }, diff --git a/src/js/sessionUtils.js b/src/js/sessionUtils.js index 70d023f0..11232e1c 100644 --- a/src/js/sessionUtils.js +++ b/src/js/sessionUtils.js @@ -38,7 +38,7 @@ var sessionUtils = (function () { curUrl = curTab.url; if (suspendMode && curUrl.indexOf('suspended.html') < 0 && !chrome.extension.getBackgroundPage().tgs.isSpecialTab(curTab)) { - curUrl = gsUtils.generateSuspendedUrl(curTab); + curUrl = gsUtils.generateSuspendedUrl(curTab.url, curTab.title); } else if (!suspendMode && curUrl.indexOf('suspended.html') > 0) { curUrl = gsUtils.getSuspendedUrl(curTab.url); } diff --git a/src/js/suspended.js b/src/js/suspended.js index 2d5727f4..505f4295 100644 --- a/src/js/suspended.js +++ b/src/js/suspended.js @@ -35,8 +35,8 @@ chrome.tabs.getCurrent(function(tab) { function attemptTabSuspend() { var url = gsUtils.getSuspendedUrl(window.location.href), - tabProperties, rootUrlStr = gsUtils.getRootUrl(url), + scrollPos = gsUtils.getSuspendedScrollPosition(window.location.href), showPreview = gsUtils.getOption(gsUtils.SCREEN_CAPTURE) !== '0', scrollImagePreview = gsUtils.getOption(gsUtils.SCREEN_CAPTURE) === '2', favicon, @@ -74,7 +74,7 @@ chrome.tabs.getCurrent(function(tab) { document.getElementById('gsPreviewImg').setAttribute('src', preview.img); if (scrollImagePreview) { document.getElementById('gsPreviewImg').addEventListener('load', function() { - document.body.scrollTop = preview.pos ? preview.pos + 60 : 0; + document.body.scrollTop = scrollPos || 0; }, { once: true }); } messageEl.style.display = 'none'; @@ -121,6 +121,7 @@ chrome.tabs.getCurrent(function(tab) { function unsuspendTab() { var url = gsUtils.getSuspendedUrl(window.location.href); + chrome.extension.getBackgroundPage().tgs.scrollPosByTabId[tabId] = gsUtils.getSuspendedScrollPosition(window.location.href); window.location.replace(url); }