Skip to content

Instantly share code, notes, and snippets.

@Aergonus
Last active February 20, 2025 17:48
Show Gist options
  • Save Aergonus/ac27048a535aeb0846f8f3fb6d79fa45 to your computer and use it in GitHub Desktop.
Save Aergonus/ac27048a535aeb0846f8f3fb6d79fa45 to your computer and use it in GitHub Desktop.
Chrome Extension that implements http://go/link URL interceptor
const BASE_URL = "https://internal.redirectservice.com/golinks";
window.RedirectRulesConfig = [
{ path: "go", host: BASE_URL },
{ path: "docs", host: `${BASE_URL}/docs-` }, // establish a standard early
{ path: "pd", host: `${BASE_URL}/pd-` }, // pagerduty links for teams/services
{ path: "ldap", host: `https://internal.redirectservice.com/ldap/?q=` },
{ path: "people", host: `https://internal.redirectservice.com/people/` },
];
const MANIFEST_JSON = {
"manifest_version": 3,
"name": "PassGo",
"version": "1.0.0",
"description": "Internal Chrome Extension to redirect golinks\nhandles:\n - `go/foo` -> `https://internal.redirectservice.com/golinks/foo`\n - `http(s)://go/bar` -> `https://internal.redirectservice.com/golinks/bar`\n - Chrome search by typing \"go\" followed by the shortname e.g. `go something`",
"permissions": [
"webNavigation",
"tabs"
],
"omnibox": {
"keyword": "go"
},
"host_permissions": [
"*://go/*",
"*://*.google.com/*",
"*://docs/*",
"*://ldap/*",
"*://pd/*",
"*://people/*"
],
"icons": {
"16": "assets/images/icon/16.png",
"32": "assets/images/icon/32.png",
"48": "assets/images/icon/48.png",
"128": "assets/images/icon/128.png"
},
"background": {
"service_worker": "assets/scripts/passgo.js"
},
"action": {
"default_icon": "assets/images/default_icon.png"
}
}
chrome.omnibox.onInputEntered.addListener(function (text) {
// omnibox only supports one keyword
// we patch in the ability to redirect go docs some-service
const [command, ...args] = text.split(' ');
const argument = args.join(' ');
const rule = window.RedirectRulesConfig.find(r => r.path === command);
if (rule) {
const newUrl = `${rule.host}${encodeURIComponent(argument)}`;
chrome.tabs.update({ url: newUrl });
} else {
const simpleRedirectUrl = `${BASE_URL}/${text}`;
chrome.tabs.update({ url: simpleRedirectUrl });
}
});
chrome.webNavigation.onBeforeNavigate.addListener(async (details) => {
try {
if (details.frameId !== 0) {
// ignore embedded iframes
return;
}
const url = new URL(details.url);
if (url.hostname.endsWith("google.com")) {
// Catch google search redirects for go/something
const goSlashRegex = /\?.*q=go%2F([^&]+)/;
const goSlashMatch = url.search.match(goSlashRegex);
if (goSlashMatch) {
const goSlashRedirectUrl = `${BASE_URL}/${match[1]}`;
chrome.tabs.update(details.tabId, { url: goSlashRedirectUrl });
}
// fallback to other custom rules
for (const rule of window.RedirectRulesConfig) {
const regexSecondary = new RegExp(`\\?.*q=${encodeURIComponent(rule.path)}%2F([^&]+)`);
const matchSecondary = url.search.match(regexSecondary);
if (matchSecondary) {
const customRedirectUrl = `${rule.host}${match[1]}`;
chrome.tabs.update(details.tabId, { url: customRedirectUrl });
}
}
} else if (url.hostname == "go") {
const customRedirectUrl = `${BASE_URL}${url.pathname}`;
chrome.tabs.update(details.tabId, { url: customRedirectUrl });
} else {
const matchingRule = window.RedirectRulesConfig.find(r => r.path === url.hostname);
if (matchingRule) {
const matchingRedirectUrl = `${matchingRule.host}${url.pathname}`;
chrome.tabs.update(details.tabId, { url: matchingRedirectUrl });
}
}
} catch (error) {
console.error("Error handling custom redirect navigation:", error);
}
});
chrome.action.onClicked.addListener((tab) => {
chrome.tabs.create({ url: BASE_URL });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment