-
Notifications
You must be signed in to change notification settings - Fork 11
/
background.js
147 lines (133 loc) · 4.13 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
var gUrlToAsyncMap = {}
var DEDUPE_KEY = "Dedupe:"
var POST_STORAGE_KEY = "Posts:"
// update on URL update
chrome.tabs.onUpdated.addListener(function(tabId, change, tab) {
console.log('onUpdated: ' + tabId)
changeAction(tab)
});
// update on selection change
chrome.tabs.onSelectionChanged.addListener(function(tabId, info) {
console.log('onSelectionChanged: ' + tabId)
chrome.tabs.getSelected(null, function(tab){
changeAction(tab)
});
});
function changeAction(tab) {
if (lscache.get(DEDUPE_KEY + tab.url + tab.id) != null) {
return // dupe
}
lscache.set(DEDUPE_KEY + tab.url + tab.id, "", 2)
isBlacklisted(tab, disableBadge, getURLInfo)
}
function getYoutubeURLs(url){
var gotVidId = false;
var video_id = '';
var urls = []
if (url.indexOf('v=') != -1) {
var video_id = url.split('v=')[1];
if (video_id != "")
gotVidId = true;
var ampersandPosition = video_id.indexOf('&');
if(ampersandPosition != -1) {
video_id = video_id.substring(0, ampersandPosition);
}
}
if (gotVidId) {
var prefixes = [
'http://www.youtube.com/watch?v=',
'https://www.youtube.com/watch?v=',
'http://www.youtu.be/',
'https://www.youtu.be/'
];
prefixes.forEach(function(prefix) {
if (prefix + video_id != url)
urls.push(prefix + video_id);
});
}
return urls;
}
function constructURLs(url){
if (url.indexOf('http') == -1) {
return []
}
var urls = [url];
if (url.indexOf('youtube.com') != -1) {
urls = urls.concat(getYoutubeURLs(url));
}
if (url.startsWith('https')) {
urls = urls.concat(url.replace('https', 'http'));
}
return urls;
}
// get URL info json
function getURLInfo(tab){
var url = tab.url
var posts = lscache.get(POST_STORAGE_KEY + url)
if (posts != null) {
console.log('cached.')
updateBadge(posts.length, tab);
return;
} else {
console.log('getURLInfo: calling reddit API')
gUrlToAsyncMap[url] = getPostsForUrl(url)
var redditPosts = []
Promise.all(gUrlToAsyncMap[url]).then(values => {
values.forEach(function(jsonData) {
redditPosts = redditPosts.concat(jsonData.data.children)
updateBadge(redditPosts.length, tab);
lscache.set(POST_STORAGE_KEY + url, redditPosts, 5)
});
});
}
}
function getPostsForUrl(url) {
var redditPosts = []
var promises = []
var urls = constructURLs(url);
console.log("checking " + urls.length)
for (var i = 0; i < urls.length; ++i) {
let queryUrl = encodeURIComponent(urls[i]);
var redditUrl = 'https://www.reddit.com/api/info.json?url=' + queryUrl;
var promise = Promise.resolve($.getJSON(redditUrl));
promises.push(promise)
}
return promises
}
function disableBadge(tab){
var title = "Blacklisted by you"
var text = "-"
var badgeColor = [175, 0, 0, 200] //red
var alienIcon = { '19': "images/alien_apathy19.png", '38': "images/alien_apathy38.png" }
setBadge(title, text, badgeColor, alienIcon, tab)
}
function updateBadge(numPosts, tab) {
var noPostsColor = [175, 0, 0, 55]
var green = [1, 175, 1, 255]
var title = "Repost"
var text = numPosts.toString()
var badgeColor = green
var alienIcon = { '19': "images/alien19.png", '38': "images/alien38.png" }
if (numPosts == 0) {
badgeColor = noPostsColor
title = "Post link"
alienIcon = { '19': "images/alien_apathy19.png", '38': "images/alien_apathy38.png" }
}
setBadge(title, text, badgeColor, alienIcon, tab)
}
function setBadge(title, text, badgeColor, alienIcon, tab) {
var tabId = tab.id
chrome.browserAction.setTitle({"title": title, "tabId": tabId})
chrome.browserAction.setBadgeBackgroundColor({
"color": badgeColor,
"tabId": tabId
})
chrome.browserAction.setBadgeText({
"text": text,
"tabId": tabId
})
chrome.browserAction.setIcon({
"path": alienIcon,
"tabId": tabId
})
}