Skip to content

Commit 2fb2ea7

Browse files
authored
Migrate extension build to WXT (#258)
* Migrate extension build to WXT * Add Firefox data collection declaration
1 parent 47082d4 commit 2fb2ea7

60 files changed

Lines changed: 13680 additions & 17920 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.babelrc

Lines changed: 0 additions & 5 deletions
This file was deleted.

.eslintrc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"env": {
3+
"browser": true,
34
"es6": true,
45
"node": true
56
},
@@ -9,7 +10,8 @@
910
"plugin:prettier/recommended"
1011
],
1112
"parserOptions": {
12-
"ecmaVersion": 2019
13+
"ecmaVersion": 2020,
14+
"sourceType": "module"
1315
},
1416
"root": true,
1517
"rules": {
@@ -22,4 +24,4 @@
2224
"no-useless-escape": "off",
2325
"prettier/prettier": "warn"
2426
}
25-
}
27+
}

.github/workflows/ci.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,5 @@ jobs:
2727
- name: Install dependencies
2828
run: npm ci
2929

30-
- name: Run lint
31-
run: npm run lint
32-
33-
- name: Run Firefox extension validation
34-
run: npm run test:firefox
30+
- name: Run validation
31+
run: npm test

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
node_modules/
2+
.output/
3+
.wxt/
24
dist/
35
packages/
46

57
.env
68
/stats
7-
/test-data
9+
/test-data

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ Contributions are always welcome! Even if you aren't comfortable coding, you can
3939

4040
### Localization/Translation
4141

42-
This extension is setup to be fully localized/translated into multiple languages, but for now English is the only language with full translations. If you are able to help localize/translate, please [check out this guide](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Internationalization). All of the text for the extension is stored [here in the `/app/_locales` directory](https://github.com/rthaut/deviantART-Filter/tree/master/app/_locales).
42+
This extension is setup to be fully localized/translated into multiple languages, but for now English is the only language with full translations. If you are able to help localize/translate, please [check out this guide](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Internationalization). All of the text for the extension is stored [here in the `/public/_locales` directory](https://github.com/rthaut/deviantART-Filter/tree/master/public/_locales).
4343

4444
### Building the Extension
4545

46-
**This extension uses the [WebExtension Toolbox](https://github.com/webextension-toolbox/webextension-toolbox#usage) for development and build processes.**
46+
**This extension uses [WXT](https://wxt.dev/) for development and build processes.**
4747

4848
To build the extension from source code, you will need to use [Node Package Manager (npm)](https://www.npmjs.com/), which handles all of the dependencies needed for this project and is used to execute the various scripts for development/building/packaging/etc.
4949

@@ -54,35 +54,37 @@ npm install
5454
Then you can run the development process (where the extension is auto-reloaded when changes are made) for your browser of choice:
5555

5656
```sh
57-
npm run dev <chrome/edge/firefox>
57+
npm run start:<chrome/edge/firefox>
5858
```
5959

6060
Or you can generate a production build for your browser of choice:
6161

6262
```sh
63-
npm run build <chrome/edge/firefox>
63+
npm run build:<chrome/edge/firefox>
64+
```
65+
66+
Or you can package the extension for your browser of choice:
67+
68+
```sh
69+
npm run zip:<chrome/edge/firefox>
6470
```
6571

6672
### Development Process
6773

68-
To make development easier, you can start up a temporary development profile on [Mozilla Firefox](https://getfirefox.com) or [Google Chrome](google.com/chrome) with the extension already loaded. The browser will also automatically detect changes and reload the extension for you (read more about this on the [`web-ext` documentation pages](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Getting_started_with_web-ext)). Use the following commands **in parallel** to re-build the extension and re-load it in Firefox/Chrome automatically as you make changes:
74+
To make development easier, you can start up a temporary development profile on [Mozilla Firefox](https://getfirefox.com) or [Google Chrome](google.com/chrome) with the extension already loaded. The browser will also automatically detect changes and reload the extension for you. Use the matching start command for your browser:
6975

7076
Firefox:
7177

7278
```sh
73-
npm run dev firefox
7479
npm run start:firefox
7580
```
7681

7782
Chrome:
7883

7984
```sh
80-
npm run dev chrome
8185
npm run start:chrome
8286
```
8387

84-
**Note that you will need 2 terminal instances**, one for each of the above commands, as they both remain running until you cancel them (use <kbd>CTRL</kbd> + <kbd>c</kbd> to cancel each process in your terminal(s)).
85-
8688

8789
[chrome-url]: https://chrome.google.com/webstore/detail/deviantart-filter/odlmamilbohnpnoomjclomghphbajikp
8890
[chrome-image-version]: https://img.shields.io/chrome-web-store/v/odlmamilbohnpnoomjclomghphbajikp?logo=googlechrome&style=for-the-badge

app/entrypoints/background.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { browser } from "wxt/browser";
2+
import { defineBackground } from "wxt/utils/define-background";
3+
4+
import { InitMenus } from "scripts/background/menus";
5+
import { OnRuntimeMessage } from "scripts/background/messages";
6+
import { OnNotificationClicked } from "scripts/background/notifications";
7+
import { OnInstalled } from "scripts/background/runtime";
8+
import { OnStorageChanged } from "scripts/background/storage";
9+
import { OpenOrShowURL, OnTabUpdate } from "scripts/background/tabs";
10+
11+
export default defineBackground({
12+
persistent: true,
13+
main() {
14+
browser.notifications.onClicked.addListener(OnNotificationClicked);
15+
browser.pageAction.onClicked.addListener(() =>
16+
OpenOrShowURL(browser.runtime.getURL("manage.html")),
17+
);
18+
browser.runtime.onInstalled.addListener(OnInstalled);
19+
browser.runtime.onMessage.addListener(OnRuntimeMessage);
20+
browser.storage.onChanged.addListener(OnStorageChanged);
21+
browser.tabs.onUpdated.addListener(OnTabUpdate);
22+
23+
InitMenus();
24+
},
25+
});
Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
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";
55

66
import {
77
LOCAL_STORAGE_CHANGED,
88
SHOW_FILTER_DEVIATION_MODAL,
99
HIDE_FILTER_DEVIATION_MODAL,
10-
} from "./constants/messages";
10+
} from "scripts/constants/messages";
11+
12+
import { PAGES } from "scripts/constants/url";
1113

12-
import { PAGES } from "./constants/url";
14+
import { SetMetadataOnNode } from "scripts/content/metadata";
1315

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+
];
1523

16-
import * as KeywordsFilter from "./content/filters/keywords";
17-
import * as UsersFilter from "./content/filters/users";
1824
const FILTERS = [KeywordsFilter, UsersFilter];
1925

2026
let ENABLED = true;
@@ -149,10 +155,7 @@ const InitFilterFrame = () => {
149155
iframe = document.createElement("iframe");
150156
iframe.setAttribute("id", id);
151157
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"));
156159
Object.assign(iframe.style, {
157160
display: "none",
158161
zIndex: 9999,
@@ -229,35 +232,37 @@ const GetPlaceholderOption = async (optionName, defaultValue) => {
229232
return data?.options?.placeholders?.[optionName] ?? defaultValue;
230233
};
231234

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();
238242

239-
ENABLED = !(await IsPageDisabled(window.location));
243+
ENABLED = !(await IsPageDisabled(window.location));
240244

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+
}
245249

246-
if (ENABLED) {
247-
document.body.classList.add("enable-metadata-indicators");
250+
if (ENABLED) {
251+
document.body.classList.add("enable-metadata-indicators");
248252

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+
}
252256

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+
}
256260

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(", "));
259263

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+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>DeviantArt Filter - Create Filters for Deviation</title>
6+
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
7+
<link rel="icon" href="/favicon.ico" type="image/x-icon" />
8+
</head>
9+
10+
<body>
11+
<div id="root"></div>
12+
<script type="module" src="../scripts/create-filters.jsx"></script>
13+
</body>
14+
</html>

app/entrypoints/manage.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<title>DeviantArt Filter Management</title>
6+
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
7+
<link rel="icon" href="/favicon.ico" type="image/x-icon" />
8+
</head>
9+
10+
<body>
11+
<div id="root"></div>
12+
<script type="module" src="../scripts/manage.jsx"></script>
13+
</body>
14+
</html>

app/manifest.json

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)