Skip to content

Commit

Permalink
Meta: Cleanup dependencies (refined-github#8233)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Jan 24, 2025
1 parent 44797fe commit b0d83b3
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 116 deletions.
72 changes: 43 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
"one-event": "^4.3.0",
"one-mutation": "^3.0.1",
"pretty-bytes": "^6.1.1",
"push-form": "^1.0.1",
"regex-join": "^2.1.0",
"select-dom": "^9.3.0",
"shorten-repo-url": "^5.2.1",
Expand All @@ -105,15 +104,16 @@
"uint8array-extras": "^1.4.0",
"webext-alert": "^1.0.3",
"webext-base-css": "^2.1.0",
"webext-bugs": "^1.1.1",
"webext-detect": "^5.3.1",
"webext-dynamic-content-scripts": "^10.0.4",
"webext-msg": "^1.0.0",
"webext-options-sync": "^4.3.0",
"webext-options-sync-per-domain": "^4.3.1",
"webext-permission-toggle": "^5.1.0",
"webext-permission-toggle": "^6.0.1",
"webext-storage": "^1.2.2",
"webext-storage-cache": "^6.0.3",
"webext-tools": "^2.0.1",
"webext-tools": "^3.0.0",
"zip-text-nodes": "^1.0.0"
},
"devDependencies": {
Expand Down
30 changes: 5 additions & 25 deletions source/background.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'webext-dynamic-content-scripts';
import 'webext-bugs/options-menu-item';
import {customizeNoAllUrlsErrorMessage} from 'webext-bugs/no-all-urls';
import {globalCache} from 'webext-storage-cache'; // Also needed to regularly clear the cache
import {addOptionsContextMenu} from 'webext-tools';
import addPermissionToggle from 'webext-permission-toggle';
import webextAlert from 'webext-alert';
import {StorageItem} from 'webext-storage';
import {handleMessages} from 'webext-msg';

Expand All @@ -20,12 +20,12 @@ const welcomeShown = new StorageItem('welcomed', {defaultValue: false});
// GHE support
addPermissionToggle();

// Firefox/Safari polyfill
addOptionsContextMenu();

// Add "Reload without content scripts" functionality
addReloadWithoutContentScripts();

// Extend the error message for the "No All URLs" bugfix
customizeNoAllUrlsErrorMessage('Refined GitHub is not meant to run on every website. If you’re looking to enable it on GitHub Enterprise, follow the instructions in the Options page.');

handleMessages({
async openUrls(urls: string[], {tab}: chrome.runtime.MessageSender) {
for (const [index, url] of urls.entries()) {
Expand Down Expand Up @@ -100,26 +100,6 @@ chrome.runtime.onInstalled.addListener(async () => {
await globalCache.clear();
}

if (await chrome.permissions.contains({origins: ['*://*/*']})) {
console.warn('Refined GitHub was granted access to all websites by the user and it’s now been removed. https://github.com/refined-github/refined-github/pull/7407');
await chrome.permissions.remove({
origins: [
'*://*/*',
],
});
}

// Call after the reset above just in case we nuked Safari's base permissions
await showWelcomePage();
});

chrome.permissions.onAdded.addListener(async permissions => {
if (permissions.origins?.includes('*://*/*')) {
await chrome.permissions.remove({
origins: [
'*://*/*',
],
});
await webextAlert('Refined GitHub is not meant to run on every website. If you’re looking to enable it on GitHub Enterprise, follow the instructions in the Options page.');
}
});
9 changes: 0 additions & 9 deletions source/helpers/dom-utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import {$, $optional} from 'select-dom/strict.js';
import {setFetch} from 'push-form';
// Nodes may be exactly `null`
import type {Nullable} from 'vitest';

// `content.fetch` is Firefox’s way to make fetches from the page instead of from a different context
// This will set the correct `origin` header without having to use XMLHttpRequest
// https://stackoverflow.com/questions/47356375/firefox-fetch-api-how-to-omit-the-origin-header-in-the-request
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts#XHR_and_Fetch
if (globalThis.content?.fetch) {
setFetch(globalThis.content.fetch);
}

/**
* Append to an element, but before a element that might not exist.
* @param parent Element (or its selector) to which append the `child`
Expand Down
22 changes: 0 additions & 22 deletions source/helpers/target-blank-polyfill.ts

This file was deleted.

2 changes: 1 addition & 1 deletion source/options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {enableTabToIndent} from 'indent-textarea';
import delegate, {type DelegateEvent} from 'delegate-it';
import {isChrome, isFirefox} from 'webext-detect';
import type {SyncedForm} from 'webext-options-sync-per-domain';
import 'webext-bugs/target-blank';

import './helpers/target-blank-polyfill.js';
import clearCacheHandler from './helpers/clear-cache-handler.js';
import {brokenFeatures, styleHotfixes} from './helpers/hotfix.js';
import {importedFeatures} from './feature-data.js';
Expand Down
42 changes: 16 additions & 26 deletions source/options/reload-without.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import webextAlert from 'webext-alert';
import chromeP from 'webext-polyfill-kinda';
import {isScriptableUrl} from 'webext-content-scripts';
import {isFirefox} from 'webext-detect';
import {createContextMenu} from 'webext-tools';

// Always Firefox… https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage/StorageArea/setAccessLevel
const area = isFirefox() ? 'local' : 'session';
Expand All @@ -12,36 +13,25 @@ export const contentScriptToggle = new StorageItem('contentScript', {
defaultValue: true,
});

const id = 'reload-without-content-scripts';

export default function addReloadWithoutContentScripts(): void {
if (!chrome.contextMenus) {
// Silently ignore if the API is not available, like in Firefox Android
// https://github.com/fregante/webext-permission-toggle/pull/53
return;
async function reload(_: unknown, tab: chrome.tabs.Tab): Promise<void> {
if (tab.url && isScriptableUrl(tab.url) && await chromeP.permissions.contains({
origins: [tab.url],
})) {
await contentScriptToggle.set(false);
chrome.tabs.reload(tab.id!);
} else {
// TODO: Use https://github.com/fregante/webext-events/issues/31 to disable the item instead
webextAlert('Refined GitHub is already not running on this page');
}
}

export default function addReloadWithoutContentScripts(): void {
chrome.storage.session.setAccessLevel?.({accessLevel: 'TRUSTED_AND_UNTRUSTED_CONTEXTS'});

chrome.contextMenus.create({
id,
void createContextMenu({
id: 'reload-without-content-scripts',
title: 'Reload without Refined GitHub',
contexts: 'action' in chrome ? ['action'] : ['browser_action'],
});

chrome.contextMenus.onClicked.addListener(async (info, tab) => {
if (!tab?.id || info.menuItemId !== id) {
return;
}

if (tab.url && isScriptableUrl(tab.url) && await chromeP.permissions.contains({
origins: [tab.url],
})) {
await contentScriptToggle.set(false);
chrome.tabs.reload(tab.id);
} else {
// TODO: Use https://github.com/fregante/webext-tools/issues/11 to disable the item instead
webextAlert('Refined GitHub is already not running on this page');
}
contexts: 'action',
onclick: reload,
});
}
2 changes: 1 addition & 1 deletion source/welcome.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<!-- prettier-ignore -->
<script lang="ts">
import {onMount} from 'svelte';
import 'webext-bugs/target-blank';
import './helpers/target-blank-polyfill.js';
import optionsStorage from './options-storage.js';
import {hasValidGitHubComToken} from './github-helpers/github-token.js';
Expand Down

0 comments on commit b0d83b3

Please sign in to comment.