forked from fabuzaid21/HN-URL-bookmarklet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
120 lines (107 loc) · 3.24 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
//key for the toggle state in the chrome local storage
TOGGLE_STATE_KEY = "toggle_state";
//getter for toggle state
function getState(callback) {
var obj = {};
obj[TOGGLE_STATE_KEY] = 0;
chrome.storage.local.get(obj, function(items) {
callback(items[TOGGLE_STATE_KEY] + 1);
});
}
//setter for toggle state
function setState(stateToSet, callback) {
var obj = {};
obj[TOGGLE_STATE_KEY] = stateToSet - 1;
chrome.storage.local.set(obj, function() {
callback();
});
}
updateAllHNTabIcons();
//handles opening new tab
//listens for messages from content script
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ? "from a content script:" + sender.tab.url : "from the extension");
var result = "did nothing";
var success = false;
if (request.say == "open webpage" && request.url !== undefined) {
getState(function(state) {
if (state == 3) {
try {
chrome.tabs.update(sender.tab.id, {url: request.url});
} catch(e) { alert(e); }
result = "opened webpage in same tab";
success = true;
} else {
var focusOnNewTab = false;
if (state == 2) focusOnNewTab = true;
try {
chrome.tabs.create({
url: request.url,
selected: focusOnNewTab,
openerTabId: sender.tab.id,
windowId: sender.tab.windowId,
index: sender.tab.index + 1
});
} catch(e) { alert(e); }
result = "opened webpage in new tab with focusOnNewTab = " + focusOnNewTab;
success = true;
}
});
} else if (request.say == "new HN tab opened") {
updateIcon(sender.tab.id);
result = "updated toggle state for new tab";
success = true;
}
sendResponse({result: result, success: success});
});
//logic for handling clicks on browser action icon
chrome.browserAction.onClicked.addListener(function(tab) {
var targetURL = "https://news.ycombinator.com/";
if (tab.url !== targetURL) { //we are not on the HN homepage
//go to the HN homepage
chrome.tabs.update(tab.id, {url: targetURL});
} else { //we are already on the HN homepage
//toggle between options 1, 2, 3
getState(function(state) {
setState((state % 3) + 1, function() {
updateAllHNTabIcons(tab.id);
});
});
}
});
//set the icon image to the icon represented by the current state
function updateIcon(tabId) {
//alert("need to update icon for tab " + tabId);
var image_19;
var image_38;
getState(function(state) {
switch(state) {
case 2:
image_19 = "images/yc_icon_19-2.png";
image_38 = "images/yc_icon_38-2.png";
break;
case 3:
image_19 = "images/yc_icon_19-3.png";
image_38 = "images/yc_icon_38-3.png";
break;
default:
image_19 = "images/yc_icon_19.png";
image_38 = "images/yc_icon_38.png";
break;
}
try {
chrome.browserAction.setIcon({path: {'19': image_19, '38': image_38}, tabId: tabId});
} catch(e) { alert(e); }
});
}
//update icon for all already open Hacker News tabs (should be called at extension launch)
function updateAllHNTabIcons() {
try {
chrome.tabs.query({url: "https://news.ycombinator.com/"}, function(HNTabs) {
for(var i = 0; i < HNTabs.length; i++) {
updateIcon(HNTabs[i].id);
}
});
} catch(e) { alert(e); }
}