-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
266 lines (237 loc) · 6.31 KB
/
popup.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
var supportSchemes;
function loadConfig() {
validSecondaryLinks = [];
validSecondaryLinks.push("https://www.chaospace.fun/links");
supportSchemes = [];
supportSchemes.push("ftp://");
supportSchemes.push("ed2k://");
supportSchemes.push("thunder://");
supportSchemes.push("magnet:?xt=urn:btih:");
supportSchemes.push("http://gdl.lixian.vip.xunlei.com");
toastr.options = {
closeButton: false,
debug: false,
newestOnTop: false,
progressBar: false,
positionClass: "toast-top-full-width",
preventDuplicates: false,
onclick: null,
showDuration: "300",
hideDuration: "1000",
timeOut: "2500",
extendedTimeOut: "1000",
showEasing: "swing",
hideEasing: "linear",
showMethod: "fadeIn",
hideMethod: "fadeOut",
};
}
var ftp = [];
var ftpName = [];
var ed2k = [];
var ed2kName = [];
var thunder = [];
var thunderName = [];
var magnet = [];
var magnetName = [];
var vip = [];
var vipName = [];
function getPrefix(url) {
for (var i = 0; i < supportSchemes.length; i++) {
if (url.startsWith(supportSchemes[i])) {
return supportSchemes[i];
}
}
return null;
}
function isSecondaryLink(url) {
for (var i = 0; i < validSecondaryLinks.length; i++) {
if (url.startsWith(validSecondaryLinks[i])) {
return true;
}
}
return false;
}
function showError(msg) {
error = msg;
updateBadgeAndTitle();
}
function insertIntoArray(prefix, link, name) {
var array;
var nameArray;
if (prefix == "ftp://") {
array = ftp;
nameArray = ftpName;
} else if (prefix == "ed2k://") {
array = ed2k;
nameArray = ed2kName;
} else if (prefix == "thunder://") {
array = thunder;
nameArray = thunderName;
} else if (prefix == "magnet:?xt=urn:btih:") {
array = magnet;
nameArray = magnetName;
} else {
array = vip;
nameArray = vipName;
}
var body = $("body");
var id = prefix.split(":")[0];
var title = $("#" + id + "-title");
if (title.length == 0) {
body.append("<h3 id='" + id + "-title'>" + id + "</h3>");
title = $("#" + id + "-title");
}
var list = $("#" + id + "-list");
if (list.length == 0) {
body.append("<ul id='" + id + "-list'></ul>");
list = $("#" + id + "-list");
}
var copyBtn = $("#" + id + "-btn");
if (copyBtn.length == 0) {
body.append("<button id='" + id + "-btn'>Copy All</button>");
copyBtn = $("#" + id + "-btn");
new Clipboard("#" + id + "-btn");
$("#" + id + "-btn").click(function () {
toastr["info"]("All links of above items are copied.");
});
}
var sendToSynoBtn = $("#" + id + "-stsbtn");
if (sendToSynoBtn.length == 0) {
body.append("<button id='" + id + "-stsbtn'>Send To My Syno</button>");
sendToSynoBtn = $("#" + id + "-stsbtn");
$("#" + id + "-stsbtn").click(function () {
urls = "";
array.forEach((v, i) => (urls += encodeURIComponent(v) + ","));
console.log("send to my synology station", urls);
sendToMySyno(urls);
});
}
if ($.inArray(link, array) == -1) {
var index = array.push(link);
nameArray.push(name);
list.append(
"<li id='" +
id +
"-li" +
index +
"' data-clipboard-text='" +
link +
"'>" +
name +
"</li>"
);
new Clipboard("#" + id + "-li" + index);
$("#" + id + "-li" + index).click(function () {
toastr["info"]("The link of this item is copied.");
});
if (copyBtn.attr("data-clipboard-text")) {
copyBtn.attr(
"data-clipboard-text",
copyBtn.attr("data-clipboard-text") + "\n" + link
);
} else {
copyBtn.attr("data-clipboard-text", link);
}
}
}
function detect(tab) {
if (!tab) {
console.log("tab empty");
return;
}
console.log("start detect");
var localRex = /^((?:chrome|file|chrome-extension|about):.*$)/i;
var result = localRex.exec(tab.url);
if (result) {
console.log("Page not supported!");
// showError("Page not supported!");
return;
}
var validRex = /^((http|https:?):.*$)/i;
result = validRex.exec(tab.url);
if (!result) {
console.log("Page not supported!");
// showError("Page not supported!");
return;
}
async function getCurrentTab() {
let queryOptions = { active: true, currentWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
return tab;
}
function parsePage(tabId) {
function getContent() {
return document.getElementsByTagName("html")[0].innerHTML;
}
chrome.scripting.executeScript(
{
target: { tabId: tabId },
func: getContent,
},
function (page) {
content = page[0].result;
function realFind(context) {
find(context, "[value]", "value");
find(context, "[href]", "href");
find(context, "[src]", "src");
}
function find(context, node, attr) {
$(context)
.find(node)
.each(function () {
var link = $(this).attr(attr);
var name = $(this).text();
if (!name) {
name = link;
}
var prefix = getPrefix(link);
if (prefix !== null) {
insertIntoArray(prefix, link, name);
}
var isValidLink = isSecondaryLink(link);
if (isValidLink) {
fetch(link)
.then((response) => response.text())
.then((response) => {
realFind(response);
});
}
});
}
// add setting btn
var body = $("body");
body.append("<div><button id='options'>Options</button></div>");
$("#options").click(function () {
console.log("open options page");
let url = chrome.runtime.getURL("options.html");
let tab = chrome.tabs.create({ url });
});
realFind(content);
}
);
}
tabPromis = getCurrentTab();
tabPromis.then((tab) => {
console.log("current tab is ", tab);
parsePage(tab.id);
});
}
window.onload = function () {
//Init
loadConfig();
chrome.action.onClicked.addListener(function (tab) {
detect(tab);
});
chrome.tabs.query(
{
active: true,
currentWindow: true,
},
function (tabs) {
if (tabs && tabs.length > 0) {
detect(tabs[0]);
}
}
);
};