From 26b1f2f4ff0ff90153658fab85b794aea4781778 Mon Sep 17 00:00:00 2001 From: violine1101 Date: Sun, 6 Nov 2022 17:05:03 +0100 Subject: [PATCH] Update to Manifest v3 and release v0.7 Note: Does not work on Firefox. Chromium-only release. --- manifest.json | 11 ++++---- package.json | 2 +- src/{background => }/background.js | 14 ++++++---- src/background/background.html | 10 ------- src/content/permissionCheck.js | 33 ++++++++++------------ src/util/badge.js | 33 +++++++++++----------- src/util/changelog.js | 34 ++++++++--------------- src/util/messages.js | 44 ++++++++++++------------------ 8 files changed, 75 insertions(+), 106 deletions(-) rename src/{background => }/background.js (80%) delete mode 100644 src/background/background.html diff --git a/manifest.json b/manifest.json index 4e96460..8e865fc 100755 --- a/manifest.json +++ b/manifest.json @@ -1,14 +1,14 @@ { - "manifest_version": 2, + "manifest_version": 3, "name": "Mojira Plus", - "version": "0.6", + "version": "0.7", "description": "Enables various additional features on Mojang's bug tracker Mojira for helpers, moderators, and other Mojira power users.", "browser_specific_settings": { "gecko": { "id": "mojira-plus@mojira.github.io", - "strict_min_version": "68.0" + "strict_min_version": "101.0" } }, @@ -18,7 +18,8 @@ }, "background": { - "page": "src/background/background.html" + "service_worker": "src/background.js", + "type": "module" }, "options_ui": { @@ -26,7 +27,7 @@ "browser_style": true }, - "browser_action": { + "action": { "default_icon": "icons/mojira-logo-32.png", "default_title": "Mojira Plus", "browser_style": true diff --git a/package.json b/package.json index ee682a0..99b8d27 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "copylibs": "(rm -rf lib || rmdir /Q /S lib || echo>NUL) && mkdir lib && (cp -r node_modules/webextension-polyfill/dist/* ./lib || copy node_modules\\webextension-polyfill\\dist\\* .\\lib)", "build": "npm run copylibs && web-ext build -o", - "lint": "npm run copylibs && web-ext lint" + "lint": "npm run copylibs && web-ext lint --firefox-preview" }, "author": "violine1101", "license": "GPL-3.0", diff --git a/src/background/background.js b/src/background.js similarity index 80% rename from src/background/background.js rename to src/background.js index c97c4c7..b336dc4 100644 --- a/src/background/background.js +++ b/src/background.js @@ -1,8 +1,10 @@ -import { initAutoupdate } from '../util/autoupdate.js'; -import { showErrorBadge } from '../util/badge.js'; -import { reportError } from '../util/errorReporting.js'; -import { getMessages, triggerMessageUpdate } from '../util/messages.js'; -import { getCustomSortIndex, getPostponeAction, getPrefix, setCustomSortIndex, setPopupMessage } from '../util/storage.js'; +import '../lib/browser-polyfill.js'; + +import { initAutoupdate } from './util/autoupdate.js'; +import { showErrorBadge } from './util/badge.js'; +import { reportError } from './util/errorReporting.js'; +import { getMessages, triggerMessageUpdate } from './util/messages.js'; +import { getCustomSortIndex, getPostponeAction, getPrefix, setCustomSortIndex, setPopupMessage } from './util/storage.js'; /** * @type {{ @@ -18,7 +20,7 @@ let permissionCache = { (async () => { await setPopupMessage(undefined); - browser.browserAction.onClicked.addListener(() => { + browser.action.onClicked.addListener(() => { browser.runtime.openOptionsPage(); }); diff --git a/src/background/background.html b/src/background/background.html deleted file mode 100644 index df3f66b..0000000 --- a/src/background/background.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - diff --git a/src/content/permissionCheck.js b/src/content/permissionCheck.js index ae56689..5a9655a 100644 --- a/src/content/permissionCheck.js +++ b/src/content/permissionCheck.js @@ -32,29 +32,24 @@ function parsePermissionResponse(response) { */ function queryPermissions() { return new Promise(async (resolve, reject) => { - const httpRequest = new XMLHttpRequest(); - httpRequest.onreadystatechange = async () => { - if (httpRequest.readyState === XMLHttpRequest.DONE) { - if (httpRequest.status === 200) { - try { - const perm = parsePermissionResponse(httpRequest.responseText); - resolve(perm); - } catch (error) { - reject(error); - } - } else if (httpRequest.status === 401) { - // User is not logged in - resolve(false); + try { + const response = await fetch('https://bugs.mojang.com/rest/api/2/myself?expand=groups'); + const text = await response.text(); + + if (response.ok) { + try { + const perm = parsePermissionResponse(text); + resolve(perm); + } catch (error) { + reject(error); } + } else if (response.status === 401) { + resolve(false); + } else { reject( - new Error(`${permissionCheckError}The server returned status code ${httpRequest.status} ${httpRequest.statusText}.`) + new Error(`${permissionCheckError}The server returned status code ${response.status} ${text}.`) ); } - } - - try { - httpRequest.open('GET', 'https://bugs.mojang.com/rest/api/2/myself?expand=groups'); - httpRequest.send(); } catch (error) { reject(`${permissionCheckError}${error}`); } diff --git a/src/util/badge.js b/src/util/badge.js index e1aefbf..dcf6b26 100644 --- a/src/util/badge.js +++ b/src/util/badge.js @@ -10,14 +10,14 @@ let currentBadge = 'none'; * @param {string} message The message that should be shown on the popup */ export async function showSuccessBadge(message) { - browser.browserAction.setPopup({ popup: '/src/popup/popup.html' }); + browser.action.setPopup({ popup: '/src/popup/popup.html' }); await setPopupMessage(message); - browser.browserAction.setBadgeBackgroundColor({ color: '#222288' }); - if (browser.browserAction.setBadgeTextColor) { - browser.browserAction.setBadgeTextColor({ color: '#ffffff' }); + browser.action.setBadgeBackgroundColor({ color: '#222288' }); + if (browser.action.setBadgeTextColor) { + browser.action.setBadgeTextColor({ color: '#ffffff' }); } - browser.browserAction.setBadgeText({ text: 'i' }); + browser.action.setBadgeText({ text: 'i' }); currentBadge = 'success'; } @@ -28,11 +28,11 @@ export async function showSuccessBadge(message) { export async function showLoadingBadge() { if (currentBadge === 'error') return; - browser.browserAction.setBadgeBackgroundColor({ color: '#228822' }); - if (browser.browserAction.setBadgeTextColor) { - browser.browserAction.setBadgeTextColor({ color: '#ffffff' }); + browser.action.setBadgeBackgroundColor({ color: '#228822' }); + if (browser.action.setBadgeTextColor) { + browser.action.setBadgeTextColor({ color: '#ffffff' }); } - browser.browserAction.setBadgeText({ text: '?' }); + browser.action.setBadgeText({ text: '?' }); currentBadge = 'loading'; } @@ -42,14 +42,14 @@ export async function showLoadingBadge() { * @param {string} message The message that should be shown on the popup */ export async function showErrorBadge(message) { - browser.browserAction.setPopup({ popup: '/src/popup/popup.html' }); + browser.action.setPopup({ popup: '/src/popup/popup.html' }); await setPopupMessage(message); - browser.browserAction.setBadgeBackgroundColor({ color: '#882222' }); - if (browser.browserAction.setBadgeTextColor) { - browser.browserAction.setBadgeTextColor({ color: '#ffffff' }); + browser.action.setBadgeBackgroundColor({ color: '#882222' }); + if (browser.action.setBadgeTextColor) { + browser.action.setBadgeTextColor({ color: '#ffffff' }); } - browser.browserAction.setBadgeText({ text: '!' }); + browser.action.setBadgeText({ text: '!' }); currentBadge = 'error'; } @@ -59,9 +59,8 @@ export async function hideBadge(delay = false) { await new Promise(resolve => setTimeout(resolve, 1000)); } - browser.browserAction.setPopup({ popup: '' }); - - browser.browserAction.setBadgeText({ text: '' }); + browser.action.setPopup({ popup: '' }); + browser.action.setBadgeText({ text: '' }); currentBadge = 'none'; } diff --git a/src/util/changelog.js b/src/util/changelog.js index b724e5a..833ae0d 100644 --- a/src/util/changelog.js +++ b/src/util/changelog.js @@ -37,29 +37,19 @@ async function updateLastCommits(response, lastKnownSha) { /** * Updates the commits since the last update */ -export function updateChangelog() { - return new Promise(async resolve => { - const lastKnownSha = await getLastCommit(); +export async function updateChangelog() { + const lastKnownSha = await getLastCommit(); - if (!(await getCommitUpdatesEnabled())) { - await setLastCommit('none'); - return; - } - - const httpRequest = new XMLHttpRequest(); - httpRequest.onreadystatechange = async () => { - if (httpRequest.readyState === XMLHttpRequest.DONE) { - if (httpRequest.status === 200) { - await updateLastCommits(httpRequest.responseText, lastKnownSha); - } else { - console.error(httpRequest); - } + if (!(await getCommitUpdatesEnabled())) { + await setLastCommit('none'); + return; + } - resolve(); - } - }; + const response = await fetch(await getCommitUrl()); - httpRequest.open('GET', await getCommitUrl()); - httpRequest.send(); - }); + if (response.ok) { + await updateLastCommits(await response.text(), lastKnownSha); + } else { + console.error(response); + } } diff --git a/src/util/messages.js b/src/util/messages.js index d690387..f6f69be 100644 --- a/src/util/messages.js +++ b/src/util/messages.js @@ -51,34 +51,26 @@ async function processMessageUpdate(response) { * Check for updated messages * @param {boolean} silent Whether the update should be executed silently (without a badge) */ -function checkForMessageUpdates(silent = false) { - return new Promise(async resolve => { - await setLastUpdateCheck(); +async function checkForMessageUpdates(silent = false) { + await setLastUpdateCheck(); - if (!silent) await showLoadingBadge(); + if (!silent) await showLoadingBadge(); - const httpRequest = new XMLHttpRequest(); - httpRequest.onreadystatechange = async () => { - if (httpRequest.readyState === XMLHttpRequest.DONE) { - if (httpRequest.status === 200) { - if (httpRequest.responseText !== await getLastCachedMessages()) { - await processMessageUpdate(httpRequest.responseText); - } else { - await hideBadge(!silent); - } - } else { - await showErrorBadge( - 'An error occurred while trying to check whether the helper messages were updated.\n' - + `The server returned status code ${httpRequest.status} ${httpRequest.statusText}.` - ); - } - resolve(); - } - }; - - httpRequest.open('GET', await getUrl()); - httpRequest.send(); - }); + const response = await fetch(await getUrl()); + const text = await response.text(); + + if (response.ok) { + if (text !== await getLastCachedMessages()) { + await processMessageUpdate(text); + } else { + await hideBadge(!silent); + } + } else { + await showErrorBadge( + 'An error occurred while trying to check whether the helper messages were updated.\n' + + `The server returned status code ${response.status} ${text}.` + ); + } } /**