Skip to content

Commit

Permalink
Fix search-index.js on-demand load mode compatiblity
Browse files Browse the repository at this point in the history
rustdoc merged a new PR [#82310](rust-lang/rust#82310) a few days ago, this cause breaking changes to this extension. This commit aims to fix this incompatibility.
  • Loading branch information
Folyd committed Mar 17, 2021
1 parent 69bae0b commit eefa192
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 43 deletions.
8 changes: 0 additions & 8 deletions extension/script/add-rustc-search-index.js

This file was deleted.

69 changes: 58 additions & 11 deletions extension/script/add-search-index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,59 @@
(function() {
// Parse crate info from location pathname.
let [_, crateVersion, crateName] = location.pathname.slice(1).split("/");
window.postMessage({
direction: "rust-search-extension",
message: {
crateName,
crateVersion,
searchIndex: window.searchIndex,
},
}, "*");
(function () {
function sendSearchIndex() {
if (location.hostname === "docs.rs") { // docs.rs pages
// Parse crate info from location pathname.
let [_, crateVersion, crateName] = location.pathname.slice(1).split("/");
window.postMessage({
direction: "rust-search-extension",
message: {
crateName,
crateVersion,
searchIndex: window.searchIndex,
},
}, "*");
} else if (location.pathname.startsWith("/nightly/nightly-rustc/")
&& location.hostname === "doc.rust-lang.org") { // rustc pages
window.postMessage({
direction: 'rust-search-extension:rustc',
message: {
searchIndex: window.searchIndex,
},
}, "*");
} else { // stable/nightly pages
const STD_CRATES = ['std', 'test', 'proc_macro'];

// Remove unnecessary std crate's search index, such as core, alloc, etc
function cleanSearchIndex() {
let searchIndex = {};
STD_CRATES.forEach(crate => {
searchIndex[crate] = window.searchIndex[crate];
});
return searchIndex;
}

window.postMessage({
direction: `rust-search-extension:std`,
message: {
searchIndex: cleanSearchIndex(window.searchIndex),
},
}, "*");
}
console.log("Send search index success.");
}

if (window.searchIndex) {
sendSearchIndex();
} else {
// Due to the new search-index.js on-demand load mode after PR #82310 has been merged.
// We need to trigger a manual search-index.js load here.
console.log("Not search index found, start loading...")
let rustdocVars = document.getElementById("rustdoc-vars");
if (rustdocVars) {
let searchJS = rustdocVars.attributes["data-search-js"].value;
let script = document.createElement('script');
script.src = searchJS;
script.onload = sendSearchIndex;
document.head.append(script);
}
}
})();
21 changes: 0 additions & 21 deletions extension/script/add-std-search-index.js

This file was deleted.

2 changes: 1 addition & 1 deletion extension/script/doc-rust-lang-org.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ document.addEventListener("DOMContentLoaded", () => {
// Check version between localStorage and today to ensure update search index once a day.
return;
}
injectScripts(["script/add-std-search-index.js"]);
injectScripts(["script/add-search-index.js"]);
}
});

Expand Down
2 changes: 1 addition & 1 deletion extension/script/rustc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ document.addEventListener("DOMContentLoaded", () => {
// Check version to ensure update search index once a day.
return;
}
injectScripts(["script/add-rustc-search-index.js"]);
injectScripts(["script/add-search-index.js"]);
}
);
});
Expand Down
2 changes: 1 addition & 1 deletion manifest.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ local json = manifest.new(
)
.addIcons(icons())
.addPermissions(['storage'])
.addWebAccessibleResources(utils.js_files('script', ['add-search-index', 'add-std-search-index', 'add-rustc-search-index']))
.addWebAccessibleResources(utils.js_files('script', ['add-search-index']))
.addBackgroundScripts(['settings.js', 'deminifier.js'])
.addBackgroundScripts(utils.js_files('search', ['algorithm', 'book', 'crate', 'attribute', 'caniuse', 'lint']))
.addBackgroundScripts(utils.js_files('search/docs', ['base', 'std', 'crate-doc', 'rustc']))
Expand Down

0 comments on commit eefa192

Please sign in to comment.