forked from greatsuspender/thegreatsuspender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuspended.js
171 lines (130 loc) · 5.54 KB
/
suspended.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*global window, document, chrome, console, Image, gsStorage */
(function () {
"use strict";
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());
};
img.src = url || chrome.extension.getURL("default.ico");
}
function getHashVariable(key) {
var parts,
temp,
i;
if (window.location.hash.length === 0) {
return false;
}
parts = window.location.hash.substring(1).split("&");
for (i = 0; i < parts.length; i++) {
temp = parts[i].split("=");
if (temp[0] === key) {
return decodeURIComponent(temp[1]);
}
}
return false;
}
function sendUnsuspendMessage(tabUrl) {
chrome.extension.sendMessage({ action: "setUnsuspendedState", tabUrl: tabUrl }, function (response) {});
}
function unsuspendTab() {
var url = getHashVariable('url');
sendUnsuspendMessage(url);
//try using tab history to go back
if (window.history.length > 1) {
window.history.back();
//otherwise try to get url from hashtag
} else if (url) {
chrome.tabs.getCurrent(function (tab) {
chrome.tabs.update(tab.id, {url: url});
});
//finally, show gs history instead (as all else has failed)
} else {
chrome.tabs.getCurrent(function (tab) {
chrome.tabs.update(tab.id, {url: chrome.extension.getURL("history.html")});
});
}
}
function suspendTab(tab, tabProperties) {
var faviconUrl,
rootUrlStr = tabProperties.url,
showPreview = gsStorage.fetchPreviewOption();
//get root of url
rootUrlStr = rootUrlStr.substring(rootUrlStr.indexOf("//") + 2);
rootUrlStr = rootUrlStr.substring(0, rootUrlStr.indexOf("/"));
document.onclick = unsuspendTab;
document.getElementById("gsTitle").innerText = tabProperties.title;
document.getElementById("gsTopBarImg").setAttribute('src', tabProperties.favicon);
document.getElementById("gsTopBarTitle").innerText = tabProperties.title;
document.getElementById("gsTopBarUrl").innerText = tabProperties.url.length > 100 ? tabProperties.url.substring(0, 100) + "..." : tabProperties.url;
document.getElementById("gsTopBarInfo").innerText = "Tab suspended: click to reload OR ";
document.getElementById("gsWhitelistLink").innerText = "add " + rootUrlStr + " to whitelist";
document.getElementById("gsWhitelistLink").setAttribute('data-text', rootUrlStr);
window.location.replace(chrome.extension.getURL("suspended.html")
+ "#url=" + encodeURIComponent(tabProperties.url));
if (showPreview) {
gsStorage.fetchPreviewImage(tabProperties.url, function (previewUrl) {
if (previewUrl !== null) {
document.getElementById("gsPreview").setAttribute('src', previewUrl);
}
});
}
generateFaviconUri(tabProperties.favicon, function (faviconUrl) {
document.getElementById("gsFavicon").setAttribute('href', faviconUrl);
});
//make sure tab is marked as suspended (may not be if reloaded from chrome restore)
tabProperties.state = 'suspended';
//update window and index information (may have changed if chrome has been restarted)
tabProperties.windowId = tab.windowId;
tabProperties.index = tab.index;
gsStorage.saveTabToHistory(tabProperties.url, tabProperties);
}
function attemptTabSuspend(tab) {
var tabProperties = gsStorage.fetchTabFromHistory(getHashVariable('url'));
//if we have some suspend information for this tab
if (tabProperties) {
console.log("about to suspend tabId: " + tab.id);
suspendTab(tab, tabProperties);
//otherwise if there is some history information then use it
} else if (window.history.length > 1) {
unsuspendTab();
//else just reload from url
} else {
chrome.tabs.update(tab.id, {url: getHashVariable('url')});
}
}
function unsuspendTabListener(request, sender, sendResponse) {
if (request.action === "unsuspendTab") {
unsuspendTab();
}
}
window.onload = function () {
//handler for unsuspend
chrome.extension.onMessage.addListener(unsuspendTabListener);
//handler for whitelist
document.getElementById("gsWhitelistLink").onclick = function (e) {
gsStorage.saveToWhitelist(e.target.getAttribute('data-text'));
};
//try to suspend tab
chrome.tabs.getCurrent(attemptTabSuspend);
};
window.onbeforeunload = function () {
chrome.tabs.getCurrent(function (tab) {
sendUnsuspendMessage(getHashVariable('url'));
});
};
window.addEventListener("keydown", function (event) {
if (event.keyCode === 13 || event.keyCode === 32 || event.keyCode === 40 || event.keyCode === 116) {
event.preventDefault();
unsuspendTab();
}
});
}());