Skip to content

Commit 3f8ef17

Browse files
committed
fix bugs
1 parent c9086de commit 3f8ef17

File tree

7 files changed

+93
-39
lines changed

7 files changed

+93
-39
lines changed

scripts/content-scripts/run_scripts.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,26 @@ function checkWillRun(script) {
6666
let url = location.href;
6767
let hasWhiteList = script.whiteList?.length > 0;
6868
let hasBlackList = script.blackList?.length > 0;
69-
let inWhiteList = matchPatterns(url, script.whiteList || []);
70-
let inBlackList = matchPatterns(url, script.blackList || []);
69+
let inWhiteList = matchOneOfPatterns(url, script.whiteList || []);
70+
let inBlackList = matchOneOfPatterns(url, script.blackList || []);
7171
return (
7272
(!hasWhiteList && !hasBlackList) ||
7373
(hasWhiteList && inWhiteList) ||
7474
(hasBlackList && !inBlackList)
7575
);
7676
}
7777

78-
function matchPatterns(url, patterns) {
78+
function matchOneOfPatterns(url, patterns) {
7979
for (let pattern of patterns) {
80-
// Replace wildcard characters * with regex wildcard .*
81-
const regexRule = pattern.replace(/\*/g, ".*");
82-
// Create a regex pattern from the rule
83-
const reg = new RegExp("^" + regexRule + "$");
84-
// Check if the URL matches the pattern
85-
if (!reg.test(url)) return false;
80+
const regex = new RegExp(
81+
"^" +
82+
pattern
83+
.split("*")
84+
.map((part) => part.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
85+
.join(".*") +
86+
"$"
87+
);
88+
if (regex.test(url)) return true;
8689
}
87-
return true;
90+
return false;
8891
}

scripts/content-scripts/scripts/ufs_global_webpage_context.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,24 @@ const UsefulScriptGlobalPageContext = {
223223
},
224224
},
225225
Utils: {
226+
formatSize(size, fixed = 0) {
227+
size = Number(size);
228+
229+
if (!size) return "?";
230+
231+
// format to KB, MB, GB
232+
if (size < 1024) {
233+
return size + "B";
234+
}
235+
if (size < 1024 * 1024) {
236+
return (size / 1024).toFixed(fixed) + "KB";
237+
}
238+
if (size < 1024 * 1024 * 1024) {
239+
return (size / (1024 * 1024)).toFixed(fixed) + "MB";
240+
}
241+
return (size / (1024 * 1024 * 1024)).toFixed(fixed) + "GB";
242+
},
243+
226244
// modified by chatgpt based on: https://gist.github.com/jcouyang/632709f30e12a7879a73e9e132c0d56b
227245
promiseAllStepN(n, list) {
228246
const head = list.slice(0, n);
@@ -394,7 +412,7 @@ const UsefulScriptGlobalPageContext = {
394412
alert("Error: " + error);
395413
}
396414
},
397-
async downloadBlobUrlWithProgress(url, progressCallback) {
415+
async getBlobFromUrlWithProgress(url, progressCallback) {
398416
const response = await fetch(url, {});
399417
if (!response.ok) {
400418
throw new Error(`Error: ${response.status} - ${response.statusText}`);

scripts/douyin_downloadAllVideoUser.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export default {
3232
"sec-fetch-mode": "cors",
3333
"sec-fetch-site": "same-origin",
3434
},
35-
referrer:
36-
"https://www.douyin.com/user/MS4wLjABAAAA5A-hCBCTdv102baOvaoZqg7nCIW_Bn_YBA0Aiz9uYPY",
35+
referrer: location.href,
36+
// "https://www.douyin.com/user/MS4wLjABAAAA5A-hCBCTdv102baOvaoZqg7nCIW_Bn_YBA0Aiz9uYPY",
3737
referrerPolicy: "strict-origin-when-cross-origin",
3838
body: null,
3939
method: "GET",
@@ -69,17 +69,22 @@ export default {
6969
console.log(moredata);
7070
hasMore = moredata["has_more"];
7171
max_cursor = moredata["max_cursor"];
72-
for (var item of moredata["aweme_list"]) {
73-
let url = item.video.play_addr.url_list[0];
72+
for (var item of moredata["aweme_list"] || []) {
73+
try {
74+
let url = item.video.play_addr.url_list[0];
7475

75-
if (url.startsWith("https")) {
76-
result.push(url);
77-
} else {
78-
result.push(url.replace("http", "https"));
76+
if (url.startsWith("https")) {
77+
result.push(url);
78+
} else {
79+
result.push(url.replace("http", "https"));
80+
}
81+
// console.clear();
82+
console.log("Number of videos: " + result.length);
83+
} catch (e) {
84+
console.log("ERROR: ", e);
7985
}
80-
// console.clear();
81-
console.log("Number of videos: " + result.length);
8286
}
87+
console.log(hasMore);
8388
}
8489
saveToFile(result.join("\n"));
8590
alert(

scripts/douyin_downloadWachingVideo.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { runScriptInCurrentTab } from "./helpers/utils.js";
1+
import { runScriptInCurrentTab, showLoading } from "./helpers/utils.js";
22

33
export default {
44
icon: "https://www.douyin.com/favicon.ico",
@@ -13,18 +13,40 @@ export default {
1313
whiteList: ["https://www.douyin.com/*"],
1414

1515
onClickExtension: async function () {
16-
const { downloadURL, downloadBlobUrl } =
17-
UsefulScriptGlobalPageContext.Utils;
16+
const {
17+
downloadURL,
18+
downloadBlob,
19+
getBlobFromUrlWithProgress,
20+
formatSize,
21+
} = UsefulScriptGlobalPageContext.Utils;
22+
23+
const { closeLoading, setLoadingText } = showLoading(
24+
"Đang tìm video url..."
25+
);
1826

1927
const src = await runScriptInCurrentTab(async () => {
2028
return await UsefulScriptGlobalPageContext.DOM.getWatchingVideoSrc();
2129
});
2230

2331
if (!src) {
2432
alert("Không tìm thấy video nào.");
25-
return;
33+
} else {
34+
setLoadingText("Đang tải video...");
35+
downloadURL(src, "douyin_video.mp4");
36+
// const blob = await getBlobFromUrlWithProgress(
37+
// src,
38+
// ({ loaded, total, speed }) => {
39+
// const percent = ((loaded / total) * 100) | 0;
40+
// setLoadingText(
41+
// `Đang tải video...<br/>` +
42+
// `Vui lòng không tắt popup <br/>` +
43+
// `${formatSize(loaded)}/${formatSize(total)} (${percent}%)` +
44+
// ` - ${formatSize(speed)}/s`
45+
// );
46+
// }
47+
// );
48+
// await downloadBlob(blob, "douyin_video.mp4");
2649
}
27-
28-
downloadBlobUrl(src, "douyin_video.mp4");
50+
closeLoading();
2951
},
3052
};

scripts/helpers/utils.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ export function checkBlackWhiteList(script, url) {
156156
b = script.blackList || [],
157157
hasWhiteList = w.length > 0,
158158
hasBlackList = b.length > 0,
159-
inWhiteList = matchPatterns(url, w) ?? true,
160-
inBlackList = matchPatterns(url, b) ?? false;
159+
inWhiteList = matchOneOfPatterns(url, w) ?? true,
160+
inBlackList = matchOneOfPatterns(url, b) ?? false;
161161

162162
let willRun =
163163
(!hasWhiteList && !hasBlackList) ||
@@ -167,16 +167,19 @@ export function checkBlackWhiteList(script, url) {
167167
return willRun;
168168
}
169169

170-
function matchPatterns(url, patterns) {
170+
function matchOneOfPatterns(url, patterns) {
171171
for (let pattern of patterns) {
172-
// Replace wildcard characters * with regex wildcard .*
173-
const regexRule = pattern.replace(/\*/g, ".*");
174-
// Create a regex pattern from the rule
175-
const reg = new RegExp("^" + regexRule + "$");
176-
// Check if the URL matches the pattern
177-
if (!reg.test(url)) return false;
172+
const regex = new RegExp(
173+
"^" +
174+
pattern
175+
.split("*")
176+
.map((part) => part.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
177+
.join(".*") +
178+
"$"
179+
);
180+
if (regex.test(url)) return true;
178181
}
179-
return true;
182+
return false;
180183
}
181184

182185
// https://stackoverflow.com/a/68634884/11898496

scripts/tiktok_downloadWatchingVideo.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export default {
1111
vi: "Tải video tiktok bạn đang xem (không watermark)",
1212
},
1313

14+
whiteList: ["https://www.tiktok.com/*"],
15+
1416
onClickExtension: async function () {
1517
const { closeLoading, setLoadingText } = showLoading("Đang lấy video id..");
1618

scripts/youtube_toggleLight.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default {
88
en: "Toggle light on/off to focus to youtube video",
99
vi: "Tắt/Mở đèn để tập trung xem video youtube",
1010
},
11-
whiteList: ["*://www.youtube.com/*"],
11+
whiteList: ["*://www.youtube.com/*"],
1212

1313
onClick: function () {
1414
["#below", "#secondary", "#masthead-container"].forEach((_) => {
@@ -21,8 +21,9 @@ export default {
2121
});
2222
});
2323

24-
document.querySelector("#player-theater-container")?.scrollIntoView?.({
24+
document.querySelector("ytd-player")?.scrollIntoView?.({
2525
behavior: "smooth",
26+
block: "center",
2627
});
2728
},
2829
};

0 commit comments

Comments
 (0)