-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.js
More file actions
99 lines (88 loc) · 3.06 KB
/
background.js
File metadata and controls
99 lines (88 loc) · 3.06 KB
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
// const videoRegex = /https:\/\/www\.tiktok\.com\/@.*\/video\/(\d*)/g;
const videoRegex =
/\bhttps?:\/\/(?:m|www|vm|vt)\.tiktok\.com\/\S*?\b(?:(?:(?:usr|v|embed|user|video)\/|\?shareId=|\&item_id=)(\d+)|(?=\w{7})(\w*?[A-Z\d]\w*)(?=\s|\/$))\b/;
const filter = {
url: [{ urlMatches: videoRegex.source }],
};
const TIKNOT_URL = "https://tiknot.netlify.app";
const sleep = () => new Promise((resolve) => setTimeout(resolve, 1000));
if (browser.contextMenus) {
// context menus are not supported in firefox for android atm, so the whole deflect toggle is disabled
browser.contextMenus.create({
id: "deflect",
title: "Deflect enabled",
contexts: ["browser_action"],
type: "checkbox",
});
browser.storage.local.get("deflect").then((data) => {
browser.contextMenus.update("deflect", {
checked: data.deflect,
});
});
browser.storage.onChanged.addListener(async (changes) => {
if (changes.deflect) {
browser.contextMenus.update("deflect", {
checked: changes.deflect.newValue,
});
}
});
browser.contextMenus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId === "deflect") {
await browser.storage.local.set({ deflect: info.checked });
}
});
}
browser.runtime.onInstalled.addListener(initStorage);
async function initStorage() {
let storage = await browser.storage.local.get();
if (storage.deflect === undefined) {
await browser.storage.local.set({ deflect: true });
}
}
async function getVideoID(url) {
const matches = url.match(videoRegex);
if (!matches) {
const urlObj = new URL(url);
if (urlObj.pathname !== "/login") {
return null;
}
const redirectUrl = urlObj.searchParams.get("redirect_url");
if (redirectUrl) {
return await getVideoID(redirectUrl);
}
} else if (matches[1]) {
return matches[1];
} else if (matches[2]) {
let response = await fetch("https://www.tikwm.com/api/?url=" + url);
await sleep(); // sleeping for 1 second to avoid rate limiting, tiknot would fail to refetch
let data = await response.json();
if (data.code === 0) {
return data.data.id;
}
}
}
browser.browserAction.onClicked.addListener(async (tab) => {
const videoID = await getVideoID(tab.url);
if (videoID) {
const url = TIKNOT_URL + "/video/" + videoID;
await browser.tabs.create({
url: url,
});
await navigator.clipboard.writeText(url);
}
});
browser.webNavigation.onBeforeNavigate.addListener(async (details) => {
const { deflect } = await browser.storage.local.get("deflect");
if (!deflect) {
return;
}
const videoID = await getVideoID(details.url);
console.log(videoID);
if (videoID) {
console.log("Tiktok video detected");
const url = TIKNOT_URL + "/video/" + videoID;
browser.tabs.update(details.tabId, {
url: url,
});
}
}, filter);