-
Notifications
You must be signed in to change notification settings - Fork 86
/
market.js
67 lines (62 loc) · 2.19 KB
/
market.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
/**
* Notify background.js that the APK downloader icon should be shown. The icon
* should be shown when a free application download is available (assume a price
* containing digits is non-free). To avoid breakage in the future, also show
* the button if the price cannot be determined.
*/
/* jshint browser:true */
/* globals chrome, console, MutationObserver, WebKitMutationObserver */
'use strict';
var shownButton;
function triggerButtonVisibilityCheck() {
var showButton = false;
if (location.pathname.lastIndexOf('/store/apps/details', 0) === 0) {
var price = document.querySelector("meta[itemprop=price]");
showButton = !price;
if (price) {
price = price.getAttribute("content");
showButton = !/\d/.test(price) || price === '0';
}
// if the Play Store pages change again...
if (!showButton && localStorage.getItem('assumeAvailable')) {
console.log('Overriding detection, assuming app available.');
showButton = true;
}
}
if (showButton !== shownButton) {
console.log("APK Downloader button will be " +
(showButton ? "shown" : "hidden"));
chrome.extension.sendMessage({
action: "showIcon",
show: showButton
});
shownButton = showButton;
}
}
if (typeof MutationObserver === 'undefined') { // Chrome 26-
window.MutationObserver = WebKitMutationObserver;
}
var observer = new MutationObserver(function() {
triggerButtonVisibilityCheck();
});
observer.observe(document.body, {
childList: true,
attributes: false,
characterData: false,
subtree: true
});
triggerButtonVisibilityCheck();
chrome.extension.onMessage.addListener(function (message, sender, sendResponse) {
var action = message && message.action;
if (action === 'checkButtonState') {
shownButton = undefined;
triggerButtonVisibilityCheck();
} else if (action === "download") {
console.log("Requested download of " + message.filename + " from " +
message.url);
var a = document.createElement("a");
a.href = message.url;
a.download = message.filename;
a.click();
}
});