This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
/
background.js
54 lines (47 loc) · 1.75 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
var lookupUrl = undefined;
var relPaymentLink = undefined;
var canonicalUrl = undefined;
// 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) {
lookupUrl = undefined;
if (isWikipedia(tab.url)) {
lookupUrl = createWikipediaAutoSubmitUrl(tab.url, tab.title);
chrome.pageAction.show(tabId);
} else {
showFlattrButtonIfThingExistsForUrl(tab.url, tabId, function(url) {
lookupUrl = url;
});
}
});
function showFlattrButtonIfThingExistsForUrl(url, tabId, callback) {
findFlattrThingForUrl(url, function(thing) {
var url;
if (thing.message === 'flattrable') {
url = 'https://flattr.com/submit/auto?url=' + escape(url);
} 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) {
if (request.relPaymentLink) {
relPaymentLink = request.relPaymentLink;
chrome.pageAction.show(sender.tab.id);
} else if (request.canonicalUrl) {
showFlattrButtonIfThingExistsForUrl(request.canonicalUrl, sender.tab.id, function(url) {
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) {
window.open(lookupUrl || relPaymentLink || canonicalUrl);
});