Skip to content

Commit 957ba84

Browse files
committed
Pretty much a rewrite to support manifest v3 (only Chrome for now)
1 parent 8b104d9 commit 957ba84

File tree

5 files changed

+125
-69
lines changed

5 files changed

+125
-69
lines changed

README.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
### Intro
22
pg_docs_bot is a browser extension for getting to the current Postgres docs by default.
3-
* [Firefox addon](https://addons.mozilla.org/en-US/firefox/addon/pg_docs_bot/)
43
* [Chrome extension](https://chrome.google.com/webstore/detail/pgdocsbot/hkbfkapgdfedgidpfbhlogecohcnaeod?hl=en-GB)
5-
* It is also trivial to install manually, if you prefer
4+
* [Firefox addon](https://addons.mozilla.org/en-US/firefox/addon/pg_docs_bot/)
5+
* It is also possible to install extensions locally, if you prefer
66

77
### Benefits
8-
When you search for Postgres related things, an old version of the documentation is often indexed. Similarly, some answers on sites like Stack Overflow link to old versions of the documentation. pg_docs_bot helps you avoid the annoyance of having to click "Current" when you get there, the frustration of having read the whole page before realising it was an old version, or even worse, not realising that it was an out of date version at all.
8+
When you search for Postgres related things, an old version of the documentation is sometimes top of the list (although this was far more of a problem several years ago). Similarly, some links on sites like Stack Overflow are to old versions of the documentation.
9+
10+
pg_docs_bot helps you avoid having to click "Current" when you get there, or the frustration of reading the information before realising it's out of date, or (even worse) not realising at all.
911

1012
### Fixing the root cause
11-
Naturally, this extension doesn't help solve the root cause(s) of this issue.
13+
There have been many conversations on the PostgreSQL mailing lists about how to teach search engines which version to link to. In 2021 a [huge improvement](https://www.postgresql.org/message-id/flat/CABUevEyGwaZE8KJg%3D-K4f7moUo%3DUbV3AFbnmtTB-c31ojNn2Vg%40mail.gmail.com#611b31dcdc09aba835ef561ad15bed69) was made to the docs to let search engines know that the `current` version should be considered canonical, which has helped tremendously.
1214

13-
There have been many conversations on the PostgreSQL mailing lists about how to teach search engines which version to link to. (June 2021 update: there is a [new thread](https://www.postgresql.org/message-id/flat/CABUevEyGwaZE8KJg%3D-K4f7moUo%3DUbV3AFbnmtTB-c31ojNn2Vg%40mail.gmail.com#611b31dcdc09aba835ef561ad15bed69) that seems promising). Please also continue to encourage people (eg on Stack Overflow or in blog posts) to link to the `current` version, which should gradually help.
15+
For the likes of Stack Overflow and blog posts, please do link to the `current` version whenever you aren't specifically referencing something version-specific.
1416

1517
### Features of pg_docs_bot
1618
* Redirects links to the `current` version
@@ -19,15 +21,9 @@ There have been many conversations on the PostgreSQL mailing lists about how to
1921
* Doesn't redirect when coming from another page in the docs, so you can still check old versions (the main problem with generic redirectors)
2022

2123
### Limitations
22-
* Only supports redirecting to `current` (request [support for other versions](https://github.com/mchristofides/pg_docs_bot/issues/1))
24+
* Only supports redirecting to `current` (here's [an alternative that does](https://github.com/dougharris/unified_docs_switcher))
2325
* Works for the English language docs
24-
* Works for version 7.0 links upwards
25-
* Chrome extension works on a limited list of search engines and Stack Exchange sites (to avoid needing <all_urls> permission)
26-
27-
### Examples
28-
Here is an example of a [search for "postgresql create index"](https://duckduckgo.com/?q=postgresql+create+index). At the time of writing, the 9.1 version of the docs is the first search result (the "current" version is 12).
29-
30-
### Requests, issues, PRs
31-
There are some feature ideas in the [issues](https://github.com/mchristofides/pg_docs_bot/issues). Bug reports and PRs super welcome.
26+
* Works for version v7.0 through v17 links
27+
* The Chrome extension only works on a limited set of search engines and Stack Exchange sites (to avoid needing the <all_urls> permission)
3228

3329
![alt text](https://github.com/mchristofides/pg_docs_bot/blob/trunk/slonik_in_glasses_128.png)

manifest.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
{
22
"name": "pg_docs_bot",
3-
"version": "1.0.3",
3+
"version": "2.0.0",
44
"description": "Redirects PostgreSQL docs links to the current version (except when coming from the docs, or to deprecated features).",
55
"homepage_url": "https://github.com/mchristofides/pg_docs_bot",
6-
"manifest_version": 2,
6+
"manifest_version": 3,
77
"icons": {
88
"48": "slonik_in_glasses_48.png",
99
"128": "slonik_in_glasses_128.png"
1010
},
1111
"permissions": [
12-
"webRequest",
13-
"webRequestBlocking",
12+
"declarativeNetRequest"
13+
],
14+
"host_permissions": [
1415
"https://www.postgresql.org/docs/*",
1516
"https://*.google.com/*",
1617
"https://*.duckduckgo.com/*",
@@ -27,6 +28,6 @@
2728
}
2829
],
2930
"background": {
30-
"scripts": ["redirectdocs.js"]
31+
"service_worker": "redirectdocs.js"
3132
}
32-
}
33+
}

manual-tests.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
### Manual tests
22

3-
* Search for, and click on, a link to an old version of the docs (should redirect to "current")
3+
* Search for "postgresql view 9.6", and click on a link to an old version of the docs (should redirect to "current")
44
* Check the the pg_docs_bot reminder notice appears
55
* Once there, try clicking on an old version (should NOT redirect back to "current")
66
* Search for "postgresql docs" and click on a link to the root URL (should work)
77
* Search for "postgresql docs faq" and click on a link to the FAQs (should work)
8-
* Search for "postgresql recovery config" and click on a link to the page (should go to the version clicked)
8+
* Search for "postgresql app-droplang" and click on a link to the page (should go to the version clicked)

pgdocsbotnotice.js

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
1-
var url = new URL(document.location);
2-
var redirectedURL = decodeURIComponent(url.searchParams.get('pg-docs-bot-redirected'));
3-
if (redirectedURL) {
4-
var redirectedURL = new URL(redirectedURL);
5-
}
6-
var redirectedPath = redirectedURL.pathname;
7-
var redirectedVersion = redirectedPath.split('/');
8-
if (String(redirectedURL).startsWith('https://www.postgresql.org/docs/')) {
9-
var hover = document.createElement('Hover');
10-
hover.innerHTML = `<div id="pgdocsbotHover" style="font-size: 14px;color: #0D0A0B;position: fixed;bottom: 30px;right: 30px;padding: 15px 30px 15px 15px;max-width: 350px;background: #BBCED9;border-radius: 5px;z-index: 100;box-shadow: 5px 5px 5px rgba(0,0,0,0.2)">Redirected by pg_docs_bot from: <a href="${redirectedURL}"><u>${redirectedVersion[2]}</u></a><button type="button" id="closeNotice" style="background: none;-webkit-appearance: none;border: none;position: absolute;right: 5px;top: 5px;line-height: 10px;color: #2c3e50;font-size: 18px;padding: 5px;cursor:pointer;">×</button></div>`;
11-
document.body.appendChild(hover);
12-
function closeButton () {
13-
document.getElementById("pgdocsbotHover").remove()
1+
function showRedirectNotice() {
2+
const url = new URL(document.location);
3+
const redirectedURLParam = url.searchParams.get('pg-docs-bot-redirected');
4+
// Avoid errors when not redirecting
5+
if (redirectedURLParam) {
6+
try {
7+
const redirectedURL = new URL(decodeURIComponent(redirectedURLParam));
8+
const redirectedPath = redirectedURL.pathname;
9+
const redirectedVersion = redirectedPath.split('/');
10+
11+
if (String(redirectedURL).startsWith('https://www.postgresql.org/docs/')) {
12+
const hover = document.createElement('div');
13+
hover.id = 'pgdocsbotHover';
14+
hover.style = `font-size: 14px;color: #0D0A0B;position: fixed;bottom: 30px;right: 30px;padding: 15px 30px 15px 15px;max-width: 350px;background: #BBCED9;border-radius: 5px;z-index: 100;box-shadow: 5px 5px 5px rgba(0,0,0,0.2)`;
15+
hover.innerHTML = `Redirected by pg_docs_bot from: <a href="${redirectedURL}"><u>${redirectedVersion[2]}</u></a><button type="button" id="closeNotice" style="background: none;-webkit-appearance: none;border: none;position: absolute;right: 5px;top: 5px;line-height: 10px;color: #2c3e50;font-size: 18px;padding: 5px;cursor:pointer;">×</button>`;
16+
document.body.appendChild(hover);
17+
document.getElementById("closeNotice").addEventListener("click", function() {
18+
document.getElementById("pgdocsbotHover").remove();
19+
});
1420
}
15-
document.getElementById("closeNotice").addEventListener("click", closeButton);
16-
};
21+
} catch (e) {
22+
// If there's an error, log it and don't show a notice
23+
console.log('pg_docs_bot: Error processing redirected URL', e);
24+
}
25+
}
26+
}
27+
// Only run when the DOM is finished loading
28+
if (document.readyState === 'loading') {
29+
document.addEventListener('DOMContentLoaded', showRedirectNotice);
30+
} else {
31+
showRedirectNotice();
32+
}

redirectdocs.js

Lines changed: 75 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,78 @@
1-
chrome.webRequest.onBeforeRequest.addListener(function(details){
2-
var pgdocsversion = 'docs\/current\/';
3-
//Firefox uses details.originUrl, Chrome uses details.initiator
4-
if (details.originUrl) {
5-
var origin = details.originUrl;
6-
}
7-
else {
8-
var origin = details.initiator;
9-
}
10-
if (origin.startsWith('https://www.postgresql.org')) {
11-
//Do not redirect when coming from postgresql.org, to allow people to deliberately view older versions
12-
return {cancel: false};
13-
}
14-
else if (/\/(archive-recovery-settings|recovery-target-settings|app-createlang|app-droplang|indexcost|inherit|manage|start-manage-db|failure|failure-disk-failed|programmer-client|developer|part-developer)\.html/.test(details.url)) {
15-
//Avoid 404s by not redirecting deprecated pages
16-
return {cancel: false};
17-
}
18-
else if (/\/release/.test(details.url)) {
19-
//Avoid redirecting release pages that sometimes redirect from current back to their own version, and seem better to not redirect in any case
20-
return {cancel: false};
1+
async function initExtension() {
2+
// Create small individual rules to keep Chrome happy
3+
const rules = [];
4+
let ruleId = 1;
5+
const versions = [
6+
'7', '7.0', '7.1', '7.2', '7.3', '7.4',
7+
'8', '8.0', '8.1', '8.2', '8.3', '8.4',
8+
'9', '9.0', '9.1', '9.2', '9.3', '9.4', '9.5', '9.6',
9+
'10', '11', '12', '13', '14', '15', '16', '17'
10+
];
11+
const deprecatedPages = [
12+
'archive-recovery-settings',
13+
'recovery-target-settings',
14+
'app-createlang',
15+
'app-droplang',
16+
'indexcost',
17+
'inherit',
18+
'manage',
19+
'start-manage-db',
20+
'failure',
21+
'failure-disk-failed',
22+
'programmer-client',
23+
'developer',
24+
'part-developer',
25+
'release'
26+
];
27+
for (const version of versions) {
28+
rules.push({
29+
id: ruleId++,
30+
priority: 1, //Lower priority than non-redirects
31+
action: {
32+
type: "redirect",
33+
redirect: {
34+
regexSubstitution: "https://www.postgresql.org/docs/current/\\1?pg-docs-bot-redirected=https://www.postgresql.org/docs/" + version + "/\\1"
35+
}
36+
},
37+
condition: {
38+
regexFilter: `^https://www\\.postgresql\\.org/docs/${version}/([^/]+\\.html)`,
39+
resourceTypes: ["main_frame"]
40+
}
41+
});
42+
}
43+
// Prevent redirects when already on postgresql.org
44+
rules.push({
45+
id: ruleId++,
46+
priority: 3, // Higher priority than redirects and deprecated pages
47+
action: {
48+
type: "allow"
49+
},
50+
condition: {
51+
urlFilter: "https://www.postgresql.org/docs/",
52+
initiatorDomains: ["postgresql.org", "www.postgresql.org"],
53+
resourceTypes: ["main_frame"]
2154
}
22-
else {
23-
//Replace version numbers 7+ as notice now makes 404s less bad. Should probably also replace devel once people can set a default.
24-
var redirectUrl = details.url.replace(/docs\/(current\/|7|7\.0|7\.1|7\.2|7\.3|7\.4|8|8\.0|8\.1|8\.2|8\.3|8\.4|9|9\.0|9\.1|9\.2|9\.3|9\.4|9\.5|9\.6|10|11|12|13)\//, pgdocsversion);
25-
if (redirectUrl === details.url) {
26-
return {cancel: false};
27-
}
28-
else {
29-
var url = new URL(redirectUrl);
30-
url.searchParams.append('pg-docs-bot-redirected', details.url);
31-
console.log(`pg_docs_bot: redirecting to ${pgdocsversion}`);
32-
return {redirectUrl: encodeURI(url)};
55+
});
56+
for (const version of versions) {
57+
for (const page of deprecatedPages) {
58+
rules.push({
59+
id: ruleId++,
60+
priority: 2, // Higher priority than redirects
61+
action: {
62+
type: "allow" // Don't redirect
63+
},
64+
condition: {
65+
urlFilter: `https://www.postgresql.org/docs/${version}/${page}.html`,
66+
resourceTypes: ["main_frame"]
3367
}
68+
});
3469
}
35-
}, {urls: ['https://www.postgresql.org/docs/*']},['blocking']);
70+
}
71+
// Register the rules
72+
await chrome.declarativeNetRequest.updateDynamicRules({
73+
removeRuleIds: Array.from({length: ruleId - 1}, (_, i) => i + 1),
74+
addRules: rules
75+
});
76+
console.log('pg_docs_bot: Registered', rules.length, 'redirect rules');
77+
}
78+
initExtension();

0 commit comments

Comments
 (0)