forked from latestchatty/chromeshack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
212 lines (184 loc) · 5.34 KB
/
background.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
function getSettings()
{
// work around chrome bug 161028
var s = {};
for (var key in localStorage)
s[key] = localStorage[key];
return s;
}
function getSetting(name, default_value)
{
var value = localStorage[name];
if (!value)
return default_value;
return JSON.parse(value);
}
function deleteSetting(name)
{
for (var key in localStorage)
{
if (key == name) {
localStorage.splice(key, 1);
}
}
}
function migrateSettings(version)
{
if (version == 1.26)
{
var derp = getSetting('lol_ugh_threshhold', false);
if (derp != false)
{
setSetting('lol_ugh_threshold', derp);
deleteSetting('lol_ugh_threshhold');
}
}
// add 'ugh' tag if not already added
if (version == 1.25 || version == 1.26)
{
var tags = getSetting("lol_tags", false);
if (tags != false)
{
var has_ugh = false;
for (var i = 0; i < tags.length; i++)
if (tags[i].name == 'ugh')
has_ugh = true;
if (!has_ugh)
{
tags.push({name: "ugh", color: "#0b0"});
setSetting('lol_tags', tags);
}
}
}
var current_version = chrome.app.getDetails().version;
if (version != current_version)
{
chrome.tabs.create({url: "release_notes.html"});
}
setSetting("version", current_version);
}
function setSetting(name, value)
{
localStorage[name] = JSON.stringify(value);
}
function showPageAction(tabId, url)
{
chrome.pageAction.setIcon({ "tabId": tabId, "path": "shack.png" });
chrome.pageAction.show(tabId);
}
function collapseThread(id)
{
var MAX_LENGTH = 100;
var collapsed = getSetting("collapsed_threads", []);
if (collapsed.indexOf(id) < 0)
{
collapsed.unshift(id);
// remove a bunch if it gets too big
if (collapsed.length > MAX_LENGTH * 1.25)
collapsed.splice(MAX_LENGTH);
setSetting("collapsed_threads", collapsed);
}
}
function unCollapseThread(id)
{
var collapsed = getSetting("collapsed_threads", []);
var index = collapsed.indexOf(id);
if (index >= 0)
{
collapsed.splice(index, 1);
setSetting("collapsed_threads", collapsed);
}
}
function addContextMenus()
{
// get rid of any old and busted context menus
chrome.contextMenus.removeAll();
// add some basic context menus
chrome.contextMenus.create(
{
title: "Show comment history",
contexts: [ 'link' ],
onclick: showCommentHistoryClick,
documentUrlPatterns: [ "http://*.shacknews.com/*" ],
targetUrlPatterns: [ "http://*.shacknews.com/profile/*" ]
});
}
function showCommentHistoryClick(info, tab)
{
var match = /\/profile\/(.+)$/.exec(info.linkUrl);
if (match)
{
var search_url = "http://winchatty.com/search?author=" + escape(match[1]);
chrome.tabs.create({windowId: tab.windowId, index: tab.index + 1, url: search_url});
}
}
var allowedIncognito = false;
//Cache this because it has to be run in the context of the extension.
chrome.extension.isAllowedIncognitoAccess(function (allowed){
allowedIncognito = allowed;
});
var lastOpenedIncognito = -1;
function openIncognito(newUrl)
{
chrome.windows.getAll({}, function(windowInfo) {
var incognitoId = -1;
if(windowInfo != null)
{
for(var i = 0; i<windowInfo.length; i++)
{
var w = windowInfo[i];
if(w.incognito)
{
incognitoId = w.id;
if(incognitoId === lastOpenedIncognito)
{
//If we found the last id we opened with, use that.
break;
}
}
}
}
if(incognitoId >= 0)
{
chrome.tabs.create({url:newUrl, windowId: incognitoId, active: false});
lastOpenedIncognito = incognitoId; //In case it wasn't opened by us.
}
else
{
//Since we can't enumerate incognito windows, the best we can do is launch a new window for each one I guess.
chrome.windows.create({url:newUrl, incognito:true, type: 'normal'}, function(windowInfo){
console.log('Window Id: ' + windowInfo.id);
lastOpenedIncognito = windowInfo.id;
});
}
});
}
chrome.extension.onMessage.addListener(function(request, sender, sendResponse)
{
if (request.name == "getSettings")
{
var tab = sender.tab;
if (tab)
showPageAction(tab.id, tab.url);
sendResponse(getSettings());
}
else if (request.name == "setSetting")
setSetting(request.key, request.value);
else if (request.name == "collapseThread")
collapseThread(request.id);
else if (request.name == "unCollapseThread")
unCollapseThread(request.id);
else if (request.name === "launchIncognito")
{
openIncognito(request.value);
sendResponse();
}
else if (request.name === 'allowedIncognitoAccess')
sendResponse(allowedIncognito);
else
sendResponse();
});
addContextMenus();
// attempt to update version settings
var last_version = getSetting("version", 0);
migrateSettings(last_version);