|
15 | 15 | along with d-comments. If not, see <https://www.gnu.org/licenses/>.
|
16 | 16 | */
|
17 | 17 |
|
18 |
| -import { type config_keys, getConfig } from "@/config"; |
19 |
| -import { threads as getThreads } from "./state"; |
| 18 | +import { getConfig } from "@/config"; |
| 19 | +import browser from "webextension-polyfill"; |
| 20 | + |
| 21 | +import comments_ngfilter from "./comments_ngfilter?worker&url"; |
| 22 | +import comments_worker from "./comments_worker?worker&url"; |
20 | 23 |
|
21 | 24 | type message = {
|
22 | 25 | comments: nv_comment[];
|
23 | 26 | };
|
24 | 27 |
|
25 |
| -const sort_worker = new Worker( |
26 |
| - new URL("./comments_worker.ts", import.meta.url), |
27 |
| - { |
28 |
| - type: "module", |
29 |
| - } |
30 |
| -); |
| 28 | +async function newWorker(url: string): Promise<Worker> { |
| 29 | + const script = await fetch(url).then((r) => r.text()); |
| 30 | + const blob = new Blob([script], { type: "application/javascript" }); |
| 31 | + const objURL = URL.createObjectURL(blob); |
| 32 | + const worker = new Worker(objURL, { type: "module" }); |
| 33 | + worker.addEventListener("error", (_e) => { |
| 34 | + URL.revokeObjectURL(objURL); |
| 35 | + }); |
| 36 | + return worker; |
| 37 | +} |
31 | 38 |
|
32 |
| -const ngfilter_worker = new Worker( |
33 |
| - new URL("./comments_ngfilter.ts", import.meta.url), |
34 |
| - { type: "module" } |
| 39 | +const ngfilter_worker = await newWorker( |
| 40 | + browser.runtime.getURL(comments_ngfilter) |
35 | 41 | );
|
| 42 | +const sort_worker = await newWorker(browser.runtime.getURL(comments_worker)); |
36 | 43 |
|
37 | 44 | async function build_ng_filter_message(comments: nv_comment[]) {
|
38 | 45 | const id = Math.random().toString(36).slice(-8);
|
39 | 46 |
|
40 | 47 | const ng_words = await getConfig("comment_ng_words");
|
41 |
| - const enabled_ng_users = ng_words.filter((w) => w.enabled); |
42 |
| - const regex = enabled_ng_users.map((w) => w.value).join("|"); |
| 48 | + const enabled_ng_words = ng_words.filter((w) => w.enabled); |
| 49 | + const regex = enabled_ng_words.map((w) => w.value).join("|"); |
43 | 50 |
|
44 | 51 | const ng_users = await getConfig("comment_ng_users");
|
45 |
| - const enabled_ng_words = ng_users |
| 52 | + const enabled_ng_users = ng_users |
46 | 53 | .filter((u) => u.enabled)
|
47 | 54 | .map((u) => u.value);
|
48 | 55 |
|
| 56 | + if (enabled_ng_words.length === 0) { |
| 57 | + return { |
| 58 | + id: id, |
| 59 | + ng_words_regex: "skip-ng-words-filter", |
| 60 | + ng_users: enabled_ng_users, |
| 61 | + comments: comments, |
| 62 | + }; |
| 63 | + } |
| 64 | + |
49 | 65 | const ng_filter_message = {
|
50 | 66 | id: id,
|
51 | 67 | ng_words_regex: new RegExp(regex, "i"),
|
52 |
| - ng_users: enabled_ng_words, |
| 68 | + ng_users: enabled_ng_users, |
53 | 69 | comments: comments,
|
54 | 70 | };
|
55 | 71 | return ng_filter_message;
|
|
0 commit comments