This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
background.js
62 lines (52 loc) · 2.02 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
var tabUrls = {};
// Listen for any changes to the URL of any tab. If URL
// exists, we show our extension icon in the address field.
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
var thisTabUrls = {};
tabUrls[tabId] = thisTabUrls;
if (isWikipedia(tab.url)) {
thisTabUrls.lookupUrl = createWikipediaAutoSubmitUrl(tab.url, tab.title);
chrome.pageAction.show(tabId);
} else {
showFlattrButtonIfThingExistsForUrl(tab.url, tabId, function(url) {
thisTabUrls.lookupUrl = url;
});
}
});
// Clean up tab information
chrome.tabs.onRemoved.addListener(function (tabId, removeInfo) {
delete tabUrls[tabId];
});
function showFlattrButtonIfThingExistsForUrl(urlToTest, tabId, callback) {
findFlattrThingForUrl(urlToTest, function(thing) {
var url;
if (thing.message === 'flattrable') {
url = 'https://flattr.com/submit/auto?url=' + escape(urlToTest);
} else if (thing) {
url = thing.link;
}
if (url) {
chrome.pageAction.show(tabId);
callback(url);
}
});
}
// See if contentscript.js finds a rel payment link or canonical url.
// Use these if the regular flattr API lookup does not find a matching thing.
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
var thisTabUrls = tabUrls[sender.tab.id];
if (request.relPaymentLink) {
thisTabUrls.relPaymentLink = request.relPaymentLink;
chrome.pageAction.show(sender.tab.id);
} else if (request.canonicalUrl) {
showFlattrButtonIfThingExistsForUrl(request.canonicalUrl, sender.tab.id, function(url) {
thisTabUrls.canonicalUrl = url;
});
}
});
// When the icon in address field is clicked, we open the flattr.com
// thing page in a new tab/window.
chrome.pageAction.onClicked.addListener(function (tab) {
var thisTabUrls = tabUrls[tab.id];
window.open(thisTabUrls.lookupUrl || thisTabUrls.relPaymentLink || thisTabUrls.canonicalUrl);
});