From 7483ba377daccb035d5b3cc9bdf88c107a86b9ee Mon Sep 17 00:00:00 2001 From: Folyd Date: Tue, 7 Dec 2021 10:23:24 +0800 Subject: [PATCH] Fix #135, docs.rs latest version path compatibility --- extension/script/add-search-index.js | 14 ++++++++++++-- extension/script/docs-rs.js | 23 +++++++++++++++++------ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/extension/script/add-search-index.js b/extension/script/add-search-index.js index 69d44d5b..124f8fc8 100644 --- a/extension/script/add-search-index.js +++ b/extension/script/add-search-index.js @@ -3,6 +3,16 @@ if (location.hostname === "docs.rs") { // docs.rs pages // Parse crate info from location pathname. let [_, crateVersion, crateName] = location.pathname.slice(1).split("/"); + // Since this PR (https://github.com/rust-lang/docs.rs/pull/1527) merged, + // the latest version path has changed: + // from https://docs.rs/tokio/1.14.0/tokio/ to https://docs.rs/tokio/latest/tokio/ + // + // If we parse the crate version from url is 'latest', + // we should reparse it from the DOM to get the correct value. + if (crateVersion === 'latest') { + let versionText = document.querySelector('nav.sidebar > div.block.version > p').textContent; + crateVersion = versionText.split(' ')[1]; + } window.postMessage({ direction: "rust-search-extension", message: { @@ -11,8 +21,8 @@ searchIndex: window.searchIndex, }, }, "*"); - } else if (location.pathname.startsWith("/nightly/nightly-rustc/") - && location.hostname === "doc.rust-lang.org") { // rustc pages + } else if (location.pathname.startsWith("/nightly/nightly-rustc/") && + location.hostname === "doc.rust-lang.org") { // rustc pages window.postMessage({ direction: 'rust-search-extension:rustc', message: { diff --git a/extension/script/docs-rs.js b/extension/script/docs-rs.js index 3abf0a5d..d72d6a3a 100644 --- a/extension/script/docs-rs.js +++ b/extension/script/docs-rs.js @@ -26,7 +26,7 @@ function highlight() { if (link) { let target = link.parentElement; target.classList.add("rse-active"); - target.scrollIntoView({behavior: "auto", block: "nearest"}); + target.scrollIntoView({ behavior: "auto", block: "nearest" }); } } }); @@ -64,6 +64,7 @@ document.addEventListener("DOMContentLoaded", () => { highlight(); }); +// Using separate event listener to avoid network requesting latency for feature flags menu enhancement. document.addEventListener("DOMContentLoaded", async () => { let menus = document.querySelector("form>.pure-menu-list:not(.pure-menu-right)"); if (!menus) return; @@ -77,14 +78,24 @@ document.addEventListener("DOMContentLoaded", async () => { } }); -// Using separate event listener to avoid network requesting latency for feature flags menu enhancement. document.addEventListener("DOMContentLoaded", async () => { let menus = document.querySelector("form>.pure-menu-list:not(.pure-menu-right)"); if (!menus) return; + // Since this PR (https://github.com/rust-lang/docs.rs/pull/1527) merged, + // the latest version path has changed: + // from https://docs.rs/tokio/1.14.0/tokio/ to https://docs.rs/tokio/latest/tokio/ + // + // If we parse the crate version from url is 'latest', + // we should reparse it from the DOM to get the correct value. + if (crateVersion === 'latest') { + let versionText = document.querySelector('nav.sidebar > div.block.version > p').textContent; + crateVersion = versionText.split(' ')[1]; + } + // Exclude /crate/** pages if (menus.children.length >= 3 && !location.pathname.includes("/crate/")) { - chrome.runtime.sendMessage({crateName, action: "crate:check"}, crate => { + chrome.runtime.sendMessage({ crateName, action: "crate:check" }, crate => { if (crate) { insertAddToExtensionElement(getState(crate.version)); } else { @@ -153,7 +164,7 @@ function insertAddToExtensionElement(state) { li.onclick = () => { // Toggle search index added state if (state === "latest") { - chrome.runtime.sendMessage({crateName, action: "crate:remove"}, response => { + chrome.runtime.sendMessage({ crateName, action: "crate:remove" }, response => { insertAddToExtensionElement(getState(undefined)); }); } else { @@ -199,7 +210,7 @@ function insertAddToExtensionElement(state) { if (menu.querySelector("#rse-here")) { menu.querySelector("#rse-here").onclick = () => { let url = chrome.runtime.getURL("manage/crates.html"); - chrome.runtime.sendMessage({action: "open-url", url}); + chrome.runtime.sendMessage({ action: "open-url", url }); }; } } @@ -208,7 +219,7 @@ window.addEventListener("message", function (event) { if (event.source === window && event.data && event.data.direction === "rust-search-extension") { - chrome.runtime.sendMessage({action: "crate:add", ...event.data.message}, + chrome.runtime.sendMessage({ action: "crate:add", ...event.data.message }, (response) => { if (response) { insertAddToExtensionElement(getState(event.data.message.crateVersion));