forked from greatsuspender/thegreatsuspender
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
601e1ad
commit 50ce2c0
Showing
25 changed files
with
474 additions
and
358 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,168 +1,188 @@ | ||
/*global chrome, window, Image */ | ||
|
||
(function () { | ||
|
||
"use strict"; | ||
|
||
var temporaryStorage = []; | ||
|
||
function killTab(tab, previewUrl, faviconUrl) { | ||
|
||
//store the tab details temporarily | ||
temporaryStorage["tab_" + tab.id] = { | ||
title: tab.title, | ||
favicon: faviconUrl, | ||
url: tab.url, | ||
preview: previewUrl | ||
}; | ||
|
||
//kill the current tab | ||
var count = 0, | ||
testLoaded = function () { | ||
chrome.tabs.get(tab.id, function (killTab) { | ||
//console.log('tab.id: '+ tab.id +' :: '+killTab.status); | ||
if (killTab.status === 'complete') { | ||
//update page to suspended.html | ||
chrome.tabs.update(tab.id, {url: chrome.extension.getURL("suspended.html")}); | ||
|
||
} else { | ||
count++; | ||
//only try for 50 * 0.1 seconds | ||
if (count < 50) { | ||
window.setTimeout(testLoaded, 100); | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
chrome.tabs.update(tab.id, {url: "chrome://kill"}); | ||
testLoaded(); | ||
} | ||
|
||
function generateFaviconUri(url, callback) { | ||
|
||
var img = new Image(); | ||
img.onload = function () { | ||
var canvas, | ||
context; | ||
canvas = window.document.createElement("canvas"); | ||
canvas.width = img.width; | ||
canvas.height = img.height; | ||
context = canvas.getContext("2d"); | ||
context.globalAlpha = 0.5; | ||
context.drawImage(img, 0, 0); | ||
callback(canvas.toDataURL()); | ||
}; | ||
//console.log(url); | ||
img.src = url || chrome.extension.getURL("default.ico"); | ||
|
||
function generatePage(tab, previewUrl, faviconUrl) { | ||
|
||
var expiryDate = (new Date()).setSeconds((new Date()).getSeconds() + 10); | ||
var html = '<title>' + tab.title + '</title>'; | ||
html += '<script type="text/javascript" >'; | ||
html += 'if (new Date() > ' + expiryDate + ') { history.back(); }'; | ||
html += 'document.onclick = function(){ history.back(); };'; | ||
html += '</script>'; | ||
html += '<link rel="icon" href="' + faviconUrl + '" />' | ||
html += '<a href="' + tab.url + '">'; | ||
if (previewUrl !== '') { | ||
html += '<img src="' + previewUrl + '" style="padding-top:40px;" />'; | ||
} | ||
html += '<div class="reloadNote" style="position: fixed;' + | ||
'color: #444;' + | ||
'text-shadow: 0 1px 0 #FFF3C5;' + | ||
'height: 40px;' + | ||
'background: #FDE073;' + | ||
'top: 0;' + | ||
'line-height: 40px;' + | ||
'width: 100%;' + | ||
'text-align: center;' + | ||
'font-family: verdana, arial, sans-serif;' + | ||
'border-bottom: 1px solid #6B5811;' + | ||
'box-shadow: 0 4px 5px -2px #555;">Tab suspended. Click to reload.' + | ||
'</div>'; | ||
html += '</a>'; | ||
html = html.replace(/\s{2,}/g, '') // <-- Replace all consecutive spaces, 2+ | ||
.replace(/%/g, '%25') // <-- Escape % | ||
.replace(/&/g, '%26') // <-- Escape & | ||
.replace(/#/g, '%23') // <-- Escape # | ||
.replace(/"/g, '%22') // <-- Escape " | ||
.replace(/'/g, '%27'); // <-- Escape ' (to be 100% safe) | ||
var dataURI = 'data:text/html,' + html; | ||
|
||
console.log('tab.id'+ tab.id +' :: generating placeholder page'); | ||
chrome.tabs.update(tab.id, {url:dataURI}); | ||
|
||
} | ||
|
||
function suspendTab(tab) { | ||
|
||
var preview = localStorage["greatsuspender.preview"] === "true" ? true : false; | ||
|
||
if (preview) { | ||
chrome.tabs.executeScript(tab.id, {file: "html2canvas.min.js"}, function() { | ||
|
||
function sendSuspendMessage(tab, preview) { | ||
|
||
chrome.tabs.executeScript(tab.id, {file: "content_script.js"}, function () { | ||
|
||
var maxHeight = window.localStorage.getItem("maxHeight") || 2, | ||
format = window.localStorage.getItem("format") || 'image/png', | ||
quality = window.localStorage.getItem("quality") || 0.4, | ||
faviconUrl, | ||
previewUrl, | ||
previewGenerated = false, | ||
faviconGenerated = false; | ||
|
||
//a little hack here to send two asynchronous requests and only handle the second response to come back | ||
chrome.tabs.sendMessage(tab.id, {preview: preview, maxHeight: maxHeight, format: format, quality: quality}, function (response) { | ||
previewUrl = response ? response.previewUrl : ''; | ||
previewGenerated = true; | ||
if (faviconGenerated !== false) { killTab(tab, previewUrl, faviconUrl); } | ||
}); | ||
|
||
generateFaviconUri("chrome://favicon/" + tab.url, function (response) { | ||
faviconUrl = response; | ||
faviconGenerated = true; | ||
if (previewGenerated !== false) { killTab(tab, previewUrl, faviconUrl); } | ||
}); | ||
}); | ||
} | ||
|
||
function suspendTab(tab) { | ||
|
||
var preview = window.localStorage.getItem("preview") === "false" ? false : true; | ||
|
||
if (preview) { | ||
chrome.tabs.executeScript(tab.id, {file: "html2canvas.min.js"}, function () { | ||
sendSuspendMessage(tab, preview); | ||
}); | ||
} else { | ||
sendSuspendMessage(tab, preview); | ||
}); | ||
} else { | ||
sendSuspendMessage(tab, preview); | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
function killTab(tab, previewUrl, faviconUrl) { | ||
|
||
var count = 0; | ||
chrome.tabs.update(tab.id, {url:"chrome://kill"}); | ||
var testLoaded = function() { | ||
chrome.tabs.get(tab.id, function(killTab) { | ||
console.log('tab.id'+ tab.id +' :: '+killTab.status); | ||
if (killTab.status === 'complete') { | ||
generatePage(tab, previewUrl, faviconUrl); | ||
|
||
} else { | ||
count++; | ||
//only try for 50 * 0.1 seconds | ||
if (count < 50) { | ||
setTimeout(testLoaded, 100); | ||
|
||
function suspendOne() { | ||
|
||
chrome.windows.getLastFocused({populate: true}, function (window) { | ||
var i; | ||
for (i = 0; i < window.tabs.length; i += 1) { | ||
if (window.tabs[i].active) { | ||
suspendTab(window.tabs[i]); | ||
} | ||
} | ||
}); | ||
} | ||
testLoaded(); | ||
} | ||
|
||
function generateFaviconUri(url, callback) { | ||
|
||
var img = new Image; | ||
img.onload = function(){ | ||
var canvas = document.createElement("canvas"); | ||
canvas.width = img.width; | ||
canvas.height = img.height; | ||
var context = canvas.getContext("2d"); | ||
context.globalAlpha = 0.5; | ||
context.drawImage(img, 0, 0); | ||
/* context.globalCompositeOperation = "darker"; | ||
context.fillStyle = "#000"; | ||
context.fillRect(0, 0, img.width, img.height); | ||
context.fill(); | ||
*/ callback(canvas.toDataURL()); | ||
}; | ||
url ? img.src = url : callback(''); | ||
|
||
} | ||
|
||
function sendSuspendMessage(tab, preview) { | ||
|
||
chrome.tabs.executeScript(tab.id, {file: "content_script.js"}, function() { | ||
|
||
|
||
var maxHeight = localStorage["greatsuspender.maxHeight"] ? localStorage["greatsuspender.maxHeight"] : 2; | ||
var format = localStorage["greatsuspender.format"] ? localStorage["greatsuspender.format"] : 'image/png'; | ||
var quality = localStorage["greatsuspender.quality"] ? +localStorage["greatsuspender.quality"] : 0.4; | ||
|
||
console.log('tab.id'+tab.id + " :: " +'sending message...'); | ||
|
||
var previewUrl = false; | ||
var faviconUrl = false; | ||
|
||
chrome.tabs.sendMessage(tab.id, {preview:preview, maxHeight:maxHeight, format:format, quality:quality}, function(response) { | ||
previewUrl = response ? response.previewUrl : ''; | ||
if (faviconUrl !== false) {killTab(tab, previewUrl, faviconUrl);} | ||
function suspendAll() { | ||
|
||
chrome.windows.getLastFocused({populate: true}, function (window) { | ||
var i; | ||
for (i = 0; i < window.tabs.length; i += 1) { | ||
if (window.tabs[i].url.indexOf("suspended.html") < 0 && window.tabs[i].url.indexOf("chrome://kill") < 0) { | ||
//console.log("tab.id"+window.tabs[i].id + " :: " +window.tabs[i].url); | ||
suspendTab(window.tabs[i]); | ||
} | ||
} | ||
}); | ||
} | ||
function unsuspendAll() { | ||
|
||
generateFaviconUri(tab.favIconUrl, function(response) { | ||
faviconUrl = response; | ||
if (previewUrl !== false) {killTab(tab, previewUrl, faviconUrl);} | ||
chrome.windows.getLastFocused({populate: true}, function (window) { | ||
var i; | ||
for (i = 0; i < window.tabs.length; i += 1) { | ||
if (window.tabs[i].url.indexOf("suspended.html") >= 0) { | ||
chrome.tabs.update(window.tabs[i].id, {url: window.tabs[i].url}); | ||
|
||
} else if (window.tabs[i].url.indexOf("chrome://kill") === 0) { | ||
chrome.tabs.reload(window.tabs[i].id); | ||
} | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
function suspendOne() { | ||
//handler for popup clicks | ||
chrome.extension.onRequest.addListener( | ||
function (request, sender, sendResponse) { | ||
|
||
chrome.windows.getCurrent({populate:true}, function(window) { | ||
var i; | ||
for (i=0; i < window.tabs.length; i += 1) { | ||
if (window.tabs[i].active) { | ||
suspendTab(window.tabs[i]); | ||
} | ||
} | ||
}); | ||
} | ||
function suspendAll() { | ||
|
||
chrome.windows.getCurrent({populate:true}, function(window) { | ||
var i; | ||
for (i=0; i < window.tabs.length; i += 1) { | ||
if (window.tabs[i].url.indexOf("data") != 0 && window.tabs[i].url.indexOf("chrome") != 0) { | ||
console.log("tab.id"+window.tabs[i].id + " :: " +window.tabs[i].url); | ||
suspendTab(window.tabs[i]); | ||
if (request.msg === "suspendOne") { | ||
suspendOne(); | ||
|
||
} else if (request.msg === "suspendAll") { | ||
suspendAll(); | ||
|
||
} else if (request.msg === "unsuspendAll") { | ||
unsuspendAll(); | ||
} | ||
} | ||
}); | ||
} | ||
function unsuspendAll() { | ||
|
||
chrome.windows.getCurrent({populate:true}, function(window) { | ||
var i; | ||
for (i=0; i < window.tabs.length; i += 1) { | ||
if (window.tabs[i].url.indexOf("data") === 0) { | ||
chrome.tabs.update(window.tabs[i].id, {url:window.tabs[i].url}); | ||
|
||
} else if (window.tabs[i].url.indexOf("chrome://kill/") === 0) { | ||
chrome.tabs.reload(window.tabs[i].id); | ||
); | ||
|
||
//handler for suspended.html onload | ||
chrome.extension.onMessage.addListener( | ||
function (request, sender, sendResponse) { | ||
if (request.action === "initialise") { | ||
|
||
var tabProperties = temporaryStorage["tab_" + sender.tab.id]; | ||
|
||
//if we have a some suspend information for this tab | ||
if (typeof tabProperties !== 'undefined') { | ||
|
||
sendResponse({ | ||
backtrack: "false", | ||
title: tabProperties.title, | ||
favicon: tabProperties.favicon, | ||
url: tabProperties.url, | ||
preview: tabProperties.preview | ||
}); | ||
|
||
//remove this entry (so that a refresh will cause original content to be loaded) | ||
delete temporaryStorage["tab_" + sender.tab.id]; | ||
|
||
//otherwise force tab to reload original content | ||
} else { | ||
sendResponse({ | ||
backtrack: "true" | ||
}); | ||
|
||
} | ||
} | ||
} | ||
}) | ||
;} | ||
|
||
chrome.extension.onRequest.addListener( | ||
function(request, sender, sendResponse){ | ||
if(request.msg == "suspendOne") suspendOne(); | ||
if(request.msg == "suspendAll") suspendAll(); | ||
if(request.msg == "unsuspendAll") unsuspendAll(); | ||
} | ||
); | ||
); | ||
|
||
}()); |
Binary file not shown.
Oops, something went wrong.