Skip to content

Commit fb590a1

Browse files
authored
Detect and avoid 404 and 500 error pages (refined-github#893)
1 parent 996f986 commit fb590a1

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

source/content.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ window.select = select;
6161

6262
async function init() {
6363
await safeElementReady('body');
64+
if (pageDetect.is404() || pageDetect.is500()) {
65+
return;
66+
}
6467
if (document.body.classList.contains('logged-out')) {
6568
return;
6669
}

source/libs/page-detect.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export const getOwnerAndRepo = () => {
2525
return {ownerName, repoName};
2626
};
2727

28-
export const is404 = () => document.title.startsWith('Page not found');
28+
export const is404 = () => document.title === 'Page not found · GitHub';
29+
30+
export const is500 = () => document.title === 'Server Error · GitHub';
2931

3032
export const isBlame = () => /^blame\//.test(getRepoPath());
3133

test/page-detect.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,31 @@ test('getOwnerAndRepo', t => {
8787
});
8888

8989
test('is404', t => {
90-
document.title = 'Page not found GitHub';
90+
document.title = 'Page not found · GitHub';
9191
t.true(pageDetect.is404());
9292

9393
document.title = 'examples/404: Page not found examples';
9494
t.false(pageDetect.is404());
9595

9696
document.title = 'Dashboard';
9797
t.false(pageDetect.is404());
98+
99+
document.title = 'Page not found · Issue #266 · sintaxi/surge · GitHub';
100+
t.false(pageDetect.is404());
101+
});
102+
103+
test('is500', t => {
104+
document.title = 'Server Error · GitHub';
105+
t.true(pageDetect.is500());
106+
107+
document.title = 'examples/500: Server Error examples';
108+
t.false(pageDetect.is500());
109+
110+
document.title = 'Dashboard';
111+
t.false(pageDetect.is500());
112+
113+
document.title = 'Server Error · Issue #266 · sintaxi/surge · GitHub';
114+
t.false(pageDetect.is500());
98115
});
99116

100117
test('isBlame', urlMatcherMacro, pageDetect.isBlame, [

0 commit comments

Comments
 (0)