Skip to content

Commit

Permalink
perf(external-link-resolver): direct link origin matching
Browse files Browse the repository at this point in the history
  • Loading branch information
Fog3211 committed Sep 19, 2024
1 parent da43ebf commit 5a899a7
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/external-link-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ interface SiteConfigs {

const siteConfigs: SiteConfigs = {
"juejin": {
directMatch: ["link.juejin.cn"],
directMatch: ["https://link.juejin.cn"],
extractTarget: async (url: URL): Promise<string | null> => {
const target = genericExtractTarget(url, "target");
if (target) {
Expand All @@ -50,7 +50,7 @@ interface SiteConfigs {
}
},
"segmentfault": {
directMatch: ["link.segmentfault.com"],
directMatch: ["https://link.segmentfault.com"],
extractTarget: async (url: URL): Promise<string> => {
return new Promise((resolve) => {
GM_xmlhttpRequest({
Expand All @@ -73,27 +73,27 @@ interface SiteConfigs {
}
},
"csdn": {
directMatch: ["link.csdn.net"],
directMatch: ["https://link.csdn.net"],
targetParam: "target"
},
"jianshu": {
directMatch: ["link.jianshu.com", "links.jianshu.com"],
directMatch: ["https://link.jianshu.com", "https://links.jianshu.com"],
targetParam: "to"
},
"zhihu": {
directMatch: ["link.zhihu.com"],
directMatch: ["https://link.zhihu.com"],
targetParam: "target"
},
"sspai": {
directMatch: ["sspai.com"],
directMatch: ["https://sspai.com"],
targetParam: "target"
}
};

async function processRule(config: SiteConfig): Promise<void> {
if (config.directMatch.includes(window.location.hostname)) {
if (config.directMatch.includes(window.location.origin)) {
let targetUrl = await getTargetUrl(config, new URL(window.location.href));
while (targetUrl && targetUrl.startsWith("https://link.juejin.cn")) {
while (targetUrl && new URL(targetUrl).origin === "https://link.juejin.cn") {
// Keep resolving until we get the final non-Juejin link
targetUrl = await getTargetUrl(siteConfigs["juejin"], new URL(targetUrl));
}
Expand All @@ -116,7 +116,7 @@ interface SiteConfigs {
}

async function replaceLinks(config: SiteConfig): Promise<void> {
const linkSelector = config.directMatch.map(site => `a[href^="https://${site}"]`).join(", ");
const linkSelector = config.directMatch.map(site => `a[href^="${site}"]`).join(", ");
const links = document.querySelectorAll<HTMLAnchorElement>(linkSelector);

for (const link of links) {
Expand All @@ -139,7 +139,7 @@ interface SiteConfigs {

async function init(): Promise<void> {
const currentSite = Object.keys(siteConfigs).find(site =>
siteConfigs[site].directMatch.includes(window.location.hostname)
siteConfigs[site].directMatch.includes(window.location.origin)
);

if (currentSite) {
Expand Down

0 comments on commit 5a899a7

Please sign in to comment.