Skip to content

Commit

Permalink
Various import/copy cleanup, avoid unnecessary library injection (#3511)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed May 30, 2022
1 parent bbcf0c0 commit e56e64c
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 182 deletions.
165 changes: 36 additions & 129 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@
"webext-additional-permissions": "^2.3.0",
"webext-content-scripts": "^1.0.1",
"webext-detect-page": "^4.0.1",
"webext-dynamic-content-scripts": "^8.0.1",
"webext-dynamic-content-scripts": "^8.1.1",
"webext-messenger": "^0.18.2",
"webext-patterns": "^1.1.1",
"webext-polyfill-kinda": "^0.10.0",
"webext-tools": "^0.3.0",
"webext-tools": "^1.0.0",
"webextension-polyfill": "^0.9.0",
"whatwg-mimetype": "^3.0.0",
"yup": "^0.32.11"
Expand Down
2 changes: 1 addition & 1 deletion src/background/activateBrowserActionIcon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@

export default function activateBrowserActionIcon() {
// This re-sets the colored manifest icons
const { icons: path } = chrome.runtime.getManifest();
const { icons: path } = browser.runtime.getManifest();
(chrome.browserAction ?? chrome.action).setIcon({ path });
}
13 changes: 5 additions & 8 deletions src/background/permissionPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import { Tabs, Windows } from "webextension-polyfill";
import { isFirefox } from "webext-detect-page";
import { isMac } from "@/utils";

const POPUP_WIDTH_PX = 400; // Makes the native prompt appear centered
const POPUP_HEIGHT_PX = 215; // Includes titlebar height, must fit the content and error to avoid scrollbars
Expand Down Expand Up @@ -46,7 +47,7 @@ async function openPopup(
url: string,
opener: Windows.Window
): Promise<Tabs.Tab> {
// `top` and `left are ignored in .create on Firefox, but attempt anyway.
// `top` and `left` are ignored in .create on Firefox, but attempt anyway.
// If present, the popup will be centered on screen rather than on the window.
// https://bugzilla.mozilla.org/show_bug.cgi?id=1396881
const window = await browser.windows.create({
Expand All @@ -65,15 +66,11 @@ async function openPopup(
/**
* Return true if popups are expected to work properly for the user agent / operating system.
*/
async function detectPopupSupport(
currentWindow: Windows.Window
): Promise<boolean> {
function detectPopupSupport(currentWindow: Windows.Window): boolean {
// Firefox on Mac seems to be unable to handle popups in fullscreen mode, changing the macOS "space"
// back to the desktop
const isBuggy =
isFirefox() &&
navigator.userAgent.includes("Macintosh") &&
currentWindow.state === "fullscreen";
isFirefox() && isMac() && currentWindow.state === "fullscreen";
return !isBuggy;
}

Expand All @@ -84,7 +81,7 @@ export async function openPopupPrompt(openerTabId: number, url: string) {
const { windowId } = await browser.tabs.get(openerTabId);
const openerWindow = await browser.windows.get(windowId);

const popupTab = (await detectPopupSupport(openerWindow))
const popupTab = detectPopupSupport(openerWindow)
? await openPopup(url, openerWindow)
: await openTab(url, openerTabId);

Expand Down
42 changes: 41 additions & 1 deletion src/blocks/available.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { checkAvailable } from "@/blocks/available";
import { checkAvailable, testMatchPatterns } from "@/blocks/available";

describe("isAvailable.urlPatterns", () => {
test("can match hash", async () => {
Expand Down Expand Up @@ -57,3 +57,43 @@ describe("isAvailable.matchPatterns", () => {
).toBe(false);
});
});

describe("testMatchPatterns", () => {
test("can match pattern", async () => {
const patterns = [
"https://www.example.com/*",
"https://*.pixiebrix.com/update/*",
];
expect(testMatchPatterns(patterns, "https://www.example.com")).toBeTrue();
expect(
testMatchPatterns(patterns, "https://pixiebrix.com/update/")
).toBeTrue();

expect(testMatchPatterns(patterns, "https://example.com")).toBeFalse();
expect(
testMatchPatterns(patterns, "https://www.example.comunication")
).toBeFalse();
expect(
testMatchPatterns(patterns, "https://www.pixiebrix.com/")
).toBeFalse();
});

test("will throw BusinessError or invalid patterns", async () => {
const url = "irrelevant";
expect(() =>
testMatchPatterns(["https://pixiebrix.*/update/*"], url)
).toThrowErrorMatchingInlineSnapshot(
'"Pattern not recognized as valid match pattern: https://pixiebrix.*/update/*"'
);
expect(() =>
testMatchPatterns(["www.example.com/*"], url)
).toThrowErrorMatchingInlineSnapshot(
'"Pattern not recognized as valid match pattern: www.example.com/*"'
);
expect(() =>
testMatchPatterns(["*.example.com/*"], url)
).toThrowErrorMatchingInlineSnapshot(
'"Pattern not recognized as valid match pattern: *.example.com/*"'
);
});
});
24 changes: 9 additions & 15 deletions src/blocks/available.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,19 @@ export function testMatchPatterns(
patterns: string[],
url: string = document.location.href
): boolean {
let re;

try {
// Try all at once
re = patternToRegex(...patterns);
} catch {
// Try them one at a time to find the broken one
for (const pattern of patterns) {
try {
patternToRegex(pattern);
} catch {
throw new BusinessError(
`Pattern not recognized as valid match pattern: ${pattern}`
);
for (const pattern of patterns) {
try {
if (patternToRegex(pattern).test(url)) {
return true;
}
} catch {
throw new BusinessError(
`Pattern not recognized as valid match pattern: ${pattern}`
);
}
}

return re.test(url);
return false;
}

function testUrlPattern(
Expand Down
Loading

0 comments on commit e56e64c

Please sign in to comment.