|
1 | | -const SELECTORS = [ |
2 | | - `a[href*="deviantart.com/"][href*="/art/"]`, |
3 | | - `a[href*="deviantart.com/"][href*="/journal/"]`, |
4 | | -]; |
| 1 | +import { browser } from "wxt/browser"; |
| 2 | +import { defineContentScript } from "wxt/utils/define-content-script"; |
| 3 | + |
| 4 | +import "../styles/content.css"; |
5 | 5 |
|
6 | 6 | import { |
7 | 7 | LOCAL_STORAGE_CHANGED, |
8 | 8 | SHOW_FILTER_DEVIATION_MODAL, |
9 | 9 | HIDE_FILTER_DEVIATION_MODAL, |
10 | | -} from "./constants/messages"; |
| 10 | +} from "scripts/constants/messages"; |
| 11 | + |
| 12 | +import { PAGES } from "scripts/constants/url"; |
11 | 13 |
|
12 | | -import { PAGES } from "./constants/url"; |
| 14 | +import { SetMetadataOnNode } from "scripts/content/metadata"; |
13 | 15 |
|
14 | | -import { SetMetadataOnNode } from "./content/metadata"; |
| 16 | +import * as KeywordsFilter from "scripts/content/filters/keywords"; |
| 17 | +import * as UsersFilter from "scripts/content/filters/users"; |
| 18 | + |
| 19 | +const SELECTORS = [ |
| 20 | + `a[href*="deviantart.com/"][href*="/art/"]`, |
| 21 | + `a[href*="deviantart.com/"][href*="/journal/"]`, |
| 22 | +]; |
15 | 23 |
|
16 | | -import * as KeywordsFilter from "./content/filters/keywords"; |
17 | | -import * as UsersFilter from "./content/filters/users"; |
18 | 24 | const FILTERS = [KeywordsFilter, UsersFilter]; |
19 | 25 |
|
20 | 26 | let ENABLED = true; |
@@ -149,10 +155,7 @@ const InitFilterFrame = () => { |
149 | 155 | iframe = document.createElement("iframe"); |
150 | 156 | iframe.setAttribute("id", id); |
151 | 157 | iframe.setAttribute("role", "dialog"); |
152 | | - iframe.setAttribute( |
153 | | - "src", |
154 | | - browser.runtime.getURL("pages/create-filters.html"), |
155 | | - ); |
| 158 | + iframe.setAttribute("src", browser.runtime.getURL("create-filters.html")); |
156 | 159 | Object.assign(iframe.style, { |
157 | 160 | display: "none", |
158 | 161 | zIndex: 9999, |
@@ -229,35 +232,37 @@ const GetPlaceholderOption = async (optionName, defaultValue) => { |
229 | 232 | return data?.options?.placeholders?.[optionName] ?? defaultValue; |
230 | 233 | }; |
231 | 234 |
|
232 | | -/** |
233 | | - * Run once the content script is loaded |
234 | | - */ |
235 | | -(async () => { |
236 | | - // create the filter frame first so it responds to messages |
237 | | - InitFilterFrame(); |
| 235 | +export default defineContentScript({ |
| 236 | + matches: ["*://*.deviantart.com/*"], |
| 237 | + allFrames: false, |
| 238 | + runAt: "document_end", |
| 239 | + async main() { |
| 240 | + // create the filter frame first so it responds to messages |
| 241 | + InitFilterFrame(); |
238 | 242 |
|
239 | | - ENABLED = !(await IsPageDisabled(window.location)); |
| 243 | + ENABLED = !(await IsPageDisabled(window.location)); |
240 | 244 |
|
241 | | - // setup message handlers as soon as we are ready to receive them |
242 | | - if (!browser.runtime.onMessage.hasListener(OnRuntimeMessage)) { |
243 | | - browser.runtime.onMessage.addListener(OnRuntimeMessage); |
244 | | - } |
| 245 | + // setup message handlers as soon as we are ready to receive them |
| 246 | + if (!browser.runtime.onMessage.hasListener(OnRuntimeMessage)) { |
| 247 | + browser.runtime.onMessage.addListener(OnRuntimeMessage); |
| 248 | + } |
245 | 249 |
|
246 | | - if (ENABLED) { |
247 | | - document.body.classList.add("enable-metadata-indicators"); |
| 250 | + if (ENABLED) { |
| 251 | + document.body.classList.add("enable-metadata-indicators"); |
248 | 252 |
|
249 | | - if (!(await GetPlaceholderOption("preventClick", true))) { |
250 | | - document.body.classList.add("clickable-placeholders"); |
251 | | - } |
| 253 | + if (!(await GetPlaceholderOption("preventClick", true))) { |
| 254 | + document.body.classList.add("clickable-placeholders"); |
| 255 | + } |
252 | 256 |
|
253 | | - if (!(await GetPlaceholderOption("showFilterText", true))) { |
254 | | - document.body.classList.add("hide-placeholder-text"); |
255 | | - } |
| 257 | + if (!(await GetPlaceholderOption("showFilterText", true))) { |
| 258 | + document.body.classList.add("hide-placeholder-text"); |
| 259 | + } |
256 | 260 |
|
257 | | - // setup observers for nodes loaded after initial render next |
258 | | - WatchForNewNodes(SELECTORS.join(", ")); |
| 261 | + // setup observers for nodes loaded after initial render next |
| 262 | + WatchForNewNodes(SELECTORS.join(", ")); |
259 | 263 |
|
260 | | - // get all existing nodes on the page and work with them |
261 | | - await HandleNodes(document.querySelectorAll(SELECTORS.join(", "))); |
262 | | - } |
263 | | -})(); |
| 264 | + // get all existing nodes on the page and work with them |
| 265 | + await HandleNodes(document.querySelectorAll(SELECTORS.join(", "))); |
| 266 | + } |
| 267 | + }, |
| 268 | +}); |
0 commit comments