Skip to content

Commit b842fc0

Browse files
committed
Opt for url regex matching over try catch
1 parent edae3ca commit b842fc0

File tree

5 files changed

+29
-19
lines changed

5 files changed

+29
-19
lines changed

public/manifest.firefox.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@
1616

1717
"content_scripts": [
1818
{
19-
"matches": ["*://github.com/*/*/blob/*"],
20-
"js": ["js/vendor.js", "js/githubFile.js"]
21-
},
22-
{
23-
"matches": ["*://github.com/*/*/pull/*/files*"],
24-
"js": ["js/vendor.js", "js/githubPR.js"]
19+
"matches": ["*://github.com/*"],
20+
"js": ["js/vendor.js", "js/githubFile.js", "js/githubPR.js"]
2521
}
2622
],
2723

public/manifest.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@
1616

1717
"content_scripts": [
1818
{
19-
"matches": ["*://github.com/*/*/blob/*"],
20-
"js": ["js/vendor.js", "js/githubFile.js"]
21-
},
22-
{
23-
"matches": ["*://github.com/*/*/pull/*/files*"],
24-
"js": ["js/vendor.js", "js/githubPR.js"]
19+
"matches": ["*://github.com/*"],
20+
"js": ["js/vendor.js", "js/githubFile.js", "js/githubPR.js"]
2521
}
2622
],
2723

src/content/github/common/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Note that we're guaranteed to be on a github page due to manifest.json,
2+
// so these checks don't need to include that.
3+
const prUrlRegex = /\/[^\/]+\/[^\/]+\/pull\/\d+\/files.*/
4+
const fileUrlRegex = /\/[^\/]+\/[^\/]+\/blob\/[^\/]+\/.*/
5+
6+
export function isFileUrl(url: string): boolean {
7+
return fileUrlRegex.test(url);
8+
}
9+
10+
export function isPrUrl(url: string): boolean {
11+
return prUrlRegex.test(url);
12+
}

src/content/github/file/main.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
FileCoverageReport,
1313
FileCoverageReportResponse,
1414
FileMetadata,
15-
MessageType,
1615
} from "src/types";
1716
import {
1817
componentsStorageKey,
@@ -34,6 +33,7 @@ import {
3433
getBranchReport,
3534
} from "../common/fetchers";
3635
import { print } from "src/utils";
36+
import { isFileUrl } from "../common/utils";
3737

3838
const globals: {
3939
coverageReport?: FileCoverageReport;
@@ -60,15 +60,14 @@ function init(): Promise<void> {
6060
}
6161

6262
async function main(): Promise<void> {
63-
let metadata: FileMetadata;
64-
65-
try {
66-
metadata = await getMetadata(document.URL);
67-
} catch (e) {
63+
if (!isFileUrl(document.URL)) {
6864
print("file not detected at current URL");
6965
return;
7066
}
7167

68+
let metadata: FileMetadata;
69+
metadata = await getMetadata(document.URL);
70+
7271
globals.coverageButton = createCoverageButton();
7372

7473
process(metadata).catch((e) => {

src/content/github/pr/main.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import _ from "lodash";
44

55
import "src/basscss.css";
66
import { displayChange } from "src/utils";
7-
import { CoverageStatus, MessageType, PullCoverageReport } from "src/types";
7+
import { CoverageStatus, PullCoverageReport } from "src/types";
88
import {
99
animateAndAnnotateLines,
1010
clearAnimation,
@@ -14,6 +14,7 @@ import { lineSelector } from "./constants";
1414
import { colors } from "../common/constants";
1515
import { print } from "src/utils";
1616
import { getPRReport } from "../common/fetchers";
17+
import { isPrUrl } from "../common/utils";
1718

1819
const globals: {
1920
coverageReport?: PullCoverageReport;
@@ -26,7 +27,13 @@ async function main() {
2627
}
2728

2829
async function execute() {
30+
if (!isPrUrl(document.URL)) {
31+
print("PR not detected at current URL");
32+
return;
33+
}
34+
2935
const urlMetadata = getMetadataFromURL();
36+
3037
if (!urlMetadata) {
3138
print("PR not detected at current URL");
3239
return;

0 commit comments

Comments
 (0)