Skip to content

Commit

Permalink
Fix #135, docs.rs latest version path compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Folyd committed Dec 7, 2021
1 parent 543f813 commit 7483ba3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
14 changes: 12 additions & 2 deletions extension/script/add-search-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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: {
Expand Down
23 changes: 17 additions & 6 deletions extension/script/docs-rs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
}
}
});
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 });
};
}
}
Expand All @@ -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));
Expand Down

0 comments on commit 7483ba3

Please sign in to comment.