Skip to content

Commit 8792cb6

Browse files
committed
support pattern lock website
1 parent 16d8776 commit 8792cb6

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

scripts/_test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ export default {
1010
},
1111

1212
popupScript: {
13+
onClick: async () => {
14+
const { openWebAndRunScript } = await import("./helpers/utils.js");
15+
openWebAndRunScript({
16+
url: "https://www.whitescreen.online/blue-screen-of-death-windows/",
17+
func: () => {
18+
setTimeout(() => {
19+
document.querySelector(".full-screen").click();
20+
}, 1000);
21+
},
22+
focusImmediately: true,
23+
waitUntilLoadEnd: true,
24+
});
25+
},
1326
_onClick: async () => {
1427
const { getCurrentTab, showLoading } = await import("./helpers/utils.js");
1528
const tab = await getCurrentTab();

scripts/auto_lockWebsite.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ export async function checkPass(reason) {
8080
return false;
8181
}
8282

83+
function matchOneOfPatterns(url, patterns) {
84+
for (let pattern of patterns) {
85+
const regex = new RegExp(
86+
pattern
87+
.split("*")
88+
.map((part) => part.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"))
89+
.join(".*")
90+
);
91+
if (regex.test(url)) return pattern;
92+
}
93+
return false;
94+
}
95+
8396
const locker = {
8497
getLockedWebsites: async () => {
8598
let key = lockedWebsiteKey;
@@ -109,7 +122,7 @@ const locker = {
109122
let pass = data?.[key];
110123
return pass;
111124
},
112-
lock: (pass) => {
125+
lock: (pass, matchedPattern) => {
113126
const id = "ufs_auto_lock_website_overlay";
114127
const idStyle = id + "-style";
115128

@@ -235,7 +248,7 @@ const locker = {
235248
inputPass.value = "";
236249

237250
if (!unlockTemporarly.checked) {
238-
locker.removeLockedWebsite(location.hostname);
251+
locker.removeLockedWebsite(matchedPattern);
239252
}
240253
}
241254
});
@@ -301,9 +314,8 @@ export default {
301314
if (pass != null) {
302315
locker.getLockedWebsites().then((websites) => {
303316
let hostname = location.hostname;
304-
if (websites.includes(hostname)) {
305-
locker.lock(pass);
306-
}
317+
let matchedPattern = matchOneOfPatterns(hostname, websites);
318+
if (matchedPattern) locker.lock(pass, matchedPattern);
307319
});
308320
}
309321
});

scripts/auto_lockWebsite_main.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ function initAddSite() {
7676
Swal.fire({
7777
title: t({ vi: "Khoá trang web", en: "Lock website" }),
7878
inputLabel: t({
79-
vi: "Nhập link trang web muốn khoá",
80-
en: "Enter website url want to lock",
79+
vi: "Nhập link/tên trang web muốn khoá\nVí dụ:\n+ facebook\n+ *.facebook.com\n+ https://www.facebook.com",
80+
en: "Enter website url/name want to lock\nE.g:\n+ facebook\n+ *.facebook.com\n+ https://www.facebook.com",
8181
}),
8282
input: "text",
8383
inputAttributes: {
@@ -99,12 +99,12 @@ function initAddSite() {
9999
if (!isConfirmed) return;
100100

101101
let hostname = value;
102-
try {
103-
let url = new URL(value);
104-
hostname = url.hostname;
105-
} catch (error) {
106-
console.log(error);
107-
}
102+
// try {
103+
// let url = new URL(value);
104+
// hostname = url.hostname;
105+
// } catch (error) {
106+
// console.log(error);
107+
// }
108108
addSite(hostname);
109109
});
110110
});

scripts/helpers/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,8 @@ export const runScriptFileInCurrentTab = async (scriptFile, world = "MAIN") => {
390390
export async function openWebAndRunScript({
391391
url,
392392
func,
393-
args,
394-
world,
393+
args = [],
394+
world = "MAIN",
395395
waitUntilLoadEnd = true,
396396
focusAfterRunScript = true,
397397
closeAfterRunScript = false,

0 commit comments

Comments
 (0)