Skip to content

Commit eefa192

Browse files
committed
Fix search-index.js on-demand load mode compatiblity
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.
1 parent 69bae0b commit eefa192

File tree

6 files changed

+61
-43
lines changed

6 files changed

+61
-43
lines changed

extension/script/add-rustc-search-index.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

extension/script/add-search-index.js

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,59 @@
1-
(function() {
2-
// Parse crate info from location pathname.
3-
let [_, crateVersion, crateName] = location.pathname.slice(1).split("/");
4-
window.postMessage({
5-
direction: "rust-search-extension",
6-
message: {
7-
crateName,
8-
crateVersion,
9-
searchIndex: window.searchIndex,
10-
},
11-
}, "*");
1+
(function () {
2+
function sendSearchIndex() {
3+
if (location.hostname === "docs.rs") { // docs.rs pages
4+
// Parse crate info from location pathname.
5+
let [_, crateVersion, crateName] = location.pathname.slice(1).split("/");
6+
window.postMessage({
7+
direction: "rust-search-extension",
8+
message: {
9+
crateName,
10+
crateVersion,
11+
searchIndex: window.searchIndex,
12+
},
13+
}, "*");
14+
} else if (location.pathname.startsWith("/nightly/nightly-rustc/")
15+
&& location.hostname === "doc.rust-lang.org") { // rustc pages
16+
window.postMessage({
17+
direction: 'rust-search-extension:rustc',
18+
message: {
19+
searchIndex: window.searchIndex,
20+
},
21+
}, "*");
22+
} else { // stable/nightly pages
23+
const STD_CRATES = ['std', 'test', 'proc_macro'];
24+
25+
// Remove unnecessary std crate's search index, such as core, alloc, etc
26+
function cleanSearchIndex() {
27+
let searchIndex = {};
28+
STD_CRATES.forEach(crate => {
29+
searchIndex[crate] = window.searchIndex[crate];
30+
});
31+
return searchIndex;
32+
}
33+
34+
window.postMessage({
35+
direction: `rust-search-extension:std`,
36+
message: {
37+
searchIndex: cleanSearchIndex(window.searchIndex),
38+
},
39+
}, "*");
40+
}
41+
console.log("Send search index success.");
42+
}
43+
44+
if (window.searchIndex) {
45+
sendSearchIndex();
46+
} else {
47+
// Due to the new search-index.js on-demand load mode after PR #82310 has been merged.
48+
// We need to trigger a manual search-index.js load here.
49+
console.log("Not search index found, start loading...")
50+
let rustdocVars = document.getElementById("rustdoc-vars");
51+
if (rustdocVars) {
52+
let searchJS = rustdocVars.attributes["data-search-js"].value;
53+
let script = document.createElement('script');
54+
script.src = searchJS;
55+
script.onload = sendSearchIndex;
56+
document.head.append(script);
57+
}
58+
}
1259
})();

extension/script/add-std-search-index.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

extension/script/doc-rust-lang-org.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ document.addEventListener("DOMContentLoaded", () => {
1616
// Check version between localStorage and today to ensure update search index once a day.
1717
return;
1818
}
19-
injectScripts(["script/add-std-search-index.js"]);
19+
injectScripts(["script/add-search-index.js"]);
2020
}
2121
});
2222

extension/script/rustc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ document.addEventListener("DOMContentLoaded", () => {
77
// Check version to ensure update search index once a day.
88
return;
99
}
10-
injectScripts(["script/add-rustc-search-index.js"]);
10+
injectScripts(["script/add-search-index.js"]);
1111
}
1212
);
1313
});

manifest.jsonnet

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ local json = manifest.new(
1414
)
1515
.addIcons(icons())
1616
.addPermissions(['storage'])
17-
.addWebAccessibleResources(utils.js_files('script', ['add-search-index', 'add-std-search-index', 'add-rustc-search-index']))
17+
.addWebAccessibleResources(utils.js_files('script', ['add-search-index']))
1818
.addBackgroundScripts(['settings.js', 'deminifier.js'])
1919
.addBackgroundScripts(utils.js_files('search', ['algorithm', 'book', 'crate', 'attribute', 'caniuse', 'lint']))
2020
.addBackgroundScripts(utils.js_files('search/docs', ['base', 'std', 'crate-doc', 'rustc']))

0 commit comments

Comments
 (0)