-
Notifications
You must be signed in to change notification settings - Fork 0
/
injectScript.js
129 lines (110 loc) · 3.45 KB
/
injectScript.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
(async () => {
if (window.CHROME_EXT_CAFEBAZAAR_DOWNLOADER_INJECTED) {
return;
}
window.CHROME_EXT_CAFEBAZAAR_DOWNLOADER_INJECTED = true;
const BUTTON_QUERY = "div.DetailsPageHeader__desktop a.AppInstallBtn";
await watchForPackage();
async function watchForPackage(btn) {
try {
if (!btn || !document.body.contains(btn)) {
btn = await waitForElement(BUTTON_QUERY);
let url = btn.getAttribute("href");
let pkg = new URL(url).searchParams.get("id");
if (url && pkg) {
await find(pkg);
}
}
} catch {}
setTimeout(() => {
watchForPackage(btn);
}, 500);
}
async function waitForElement(selector) {
return new Promise(function (resolve, reject) {
let element = document.querySelector(selector);
if (element) {
resolve(element);
return;
}
const interval = setInterval(() => {
let element = document.querySelector(selector);
if (element) {
clearInterval(interval);
resolve(element);
return;
}
}, 100);
});
}
async function find(pkg) {
try {
let response = await findDownloadLink(pkg);
let res = await response.json();
if (!res.singleReply || !res.singleReply.appDownloadInfoReply) {
console.log("Invalid response:", res);
return;
}
let token = res.singleReply.appDownloadInfoReply.token;
let cdnPrefix = res.singleReply.appDownloadInfoReply.cdnPrefix[0];
let packageSize =
res.singleReply.appDownloadInfoReply.packageSize / 1024 / 1024;
let versionCode = res.singleReply.appDownloadInfoReply.versionCode || 0;
let downloadLink = `${cdnPrefix}apks/${token}.apk`;
console.log("APK download link:", downloadLink);
await changeButton(downloadLink, packageSize, versionCode, token, pkg);
} catch (error) {
console.log("Request failed", error);
}
}
async function findDownloadLink(pkg) {
return fetch(
"https://api.cafebazaar.ir/rest-v1/process/AppDownloadInfoRequest",
{
mode: "cors",
method: "post",
headers: {
Accept: "application/json",
"Content-type": "application/json",
},
body: JSON.stringify({
properties: {
language: 2,
clientVersionCode: 1100301,
androidClientInfo: {
sdkVersion: 22,
cpu: "x86,armeabi-v7a,armeabi",
},
clientVersion: "11.3.1",
isKidsEnabled: false,
},
singleRequest: {
appDownloadInfoRequest: {
downloadStatus: 1,
packageName: pkg,
referrers: [],
},
},
}),
}
);
}
async function changeButton(
downloadLink,
packageSize,
versionCode,
token,
pkg
) {
let btn = await waitForElement(BUTTON_QUERY);
let downloadBtn = document.createElement("a");
downloadBtn.setAttribute("class", "AppInstallBtn newbtn");
downloadBtn.setAttribute("href", downloadLink);
downloadBtn.setAttribute("title", `${token}_${pkg}_${versionCode}.apk`);
downloadBtn.setAttribute("data-color", "primary");
downloadBtn.setAttribute("data-size", "lg");
downloadBtn.innerHTML = `⬇️ دانلود (${packageSize.toFixed(2)} MB)`;
btn.parentNode.insertBefore(downloadBtn, btn.parentNode.childNodes[0]);
btn.parentNode.removeChild(btn);
}
})();