Skip to content

Commit

Permalink
Store scroll position in suspended url args (#537)
Browse files Browse the repository at this point in the history
  • Loading branch information
deanoemcke committed Jul 25, 2017
1 parent 72567c4 commit 019322a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 99 deletions.
71 changes: 27 additions & 44 deletions src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -97,25 +97,27 @@ 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)
});
});

} else {
sendMessageToTab(tab.id, {
action: 'confirmTabSuspend',
suspendedUrl: gsUtils.generateSuspendedUrl(tab)
suspendedUrl: gsUtils.generateSuspendedUrl(tab.url, tab.title, scrollPos)
});
}
});
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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});
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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':
Expand All @@ -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':
Expand All @@ -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);
Expand Down Expand Up @@ -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
};

}());
Expand Down
66 changes: 20 additions & 46 deletions src/js/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
(function () {
'use strict';

var tabId,
readyStateCheckInterval,
var readyStateCheckInterval,
inputState = false,
tempWhitelist = false,
timerJob,
Expand All @@ -24,55 +23,47 @@
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
if (response && response.dontSuspendForms) {
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() {
var status = inputState ? 'formInput' : (tempWhitelist ? 'tempWhitelist' : 'normal');
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);
Expand All @@ -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) {

Expand Down Expand Up @@ -136,10 +125,9 @@
chrome.runtime.sendMessage({
action: 'savePreviewData',
previewUrl: dataUrl,
position: position,
timerMsg: timer
}, function () {
suspendTab(suspendedUrl, position);
suspendTab(suspendedUrl);
});
}
}
Expand Down Expand Up @@ -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() + ']');

Expand All @@ -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
Expand All @@ -242,17 +220,13 @@
status = inputState ? 'formInput' : (tempWhitelist ? 'tempWhitelist' : 'normal');
response = {status: status};
tempWhitelist = true;
reportState(false);
sendResponse(response);
break;

//listen for request to undo temporary whitelisting
case 'undoTempWhitelist':
inputState = false;
tempWhitelist = false;
response = {status: 'normal'};
reportState(false);
sendResponse(response);
break;

//listen for preview request
Expand Down
15 changes: 9 additions & 6 deletions src/js/gsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@
});
},

addPreviewImage: function (tabUrl, previewUrl, position) {
addPreviewImage: function (tabUrl, previewUrl) {
var self = this,
server;
this.getDb().then(function (s) {
Expand All @@ -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});
});
},

Expand Down Expand Up @@ -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);
},
Expand Down Expand Up @@ -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);
},
Expand Down
2 changes: 1 addition & 1 deletion src/js/sessionUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading

0 comments on commit 019322a

Please sign in to comment.