Created
February 22, 2022 01:57
-
-
Save juneoh/c027d06bf91a945364df317c34151d8b to your computer and use it in GitHub Desktop.
A crude but working web extension for trot.to, both for macOS and iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log("Loaded background.js"); | |
const providerURL = "https://trot.to/"; | |
const pattern = /https*:\/\/go\/(.*)/; | |
const mobileErrorPage = "data:text/html,"; | |
const isMobile = navigator.standalone === false; | |
let path = ""; | |
let tabId = 0; | |
function logState(phase) { | |
console.log(`${phase}, ${tabId}, ${path}`); | |
} | |
function openOnTab() { | |
logState("open"); | |
browser.tabs.update(tabId, {url: providerURL + path}); | |
} | |
function detectFromBeforeNavigate(event) { | |
if (event.frameId != 0 || event.parentFrameId != -1) return; | |
// For iOS: open after the error page to avoid being ignored | |
if (isMobile && event.url == mobileErrorPage && path) { | |
logState("mobile error"); | |
openOnTab(); | |
path = ""; | |
} | |
let result = pattern.exec(event.url); | |
if (result) { | |
path = result[1]; | |
tabId = event.tabId; | |
logState("detect"); | |
// For macOS | |
if (!isMobile) { | |
// event.tabId can be 0 during load | |
if (!tabId) { | |
browser.tabs.getCurrent().then(function (tab) { | |
tabId = tab.id; | |
openOnTab(); | |
}); | |
} else { | |
openOnTab(); | |
} | |
} | |
// For iOS: rewrite Google searches | |
} else if (isMobile) { | |
const url = new URL(event.url); | |
if (url.hostname == "www.google.co.kr" || url.hostname == "www.google.com") { | |
const query = url.searchParams.get("q"); | |
if (query && query.substring(0, 3) == "go/") { | |
path = query.substring(3); | |
tabId = event.tabId; | |
openOnTab(); | |
} | |
} | |
} | |
} | |
browser.webNavigation.onBeforeNavigate.addListener(detectFromBeforeNavigate); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment