Add nightly search index

This commit is contained in:
Folyd 2020-08-21 11:27:48 +08:00
parent a42bb973e8
commit 756b96dca3
5 changed files with 59 additions and 10 deletions

View file

@ -212,6 +212,22 @@ chrome.runtime.setUninstallURL(
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
switch (message.action) {
// Nightly:* action is exclusive to nightly docs event
case "nightly:check" : {
let currentNightlyVersion = NightlyDocManager.getNightlyVersion();
if (currentNightlyVersion && Date.parse(currentNightlyVersion) >= Date.parse(message.nightlyVersion)) {
sendResponse({state: 'latest'});
} else {
sendResponse({state: 'outdated'});
}
break;
}
case "nightly:add" : {
NightlyDocManager.setNightlyDocs(message.searchIndex);
NightlyDocManager.setNightlyVersion(message.nightlyVersion);
sendResponse(true);
break;
}
case "check": {
let crates = CrateDocSearchManager.getCrates();
sendResponse(crates[message.crateName]);

View file

@ -1,4 +1,4 @@
(function() {
(function () {
// Remove needless crate's search index, such as core, alloc, etc
function cleanSearchIndex() {
let searchIndex = {};
@ -7,9 +7,9 @@
searchIndex['proc_macro'] = window.searchIndex['proc_macro'];
return searchIndex;
}
let p = document.querySelector('nav.sidebar>div.version>p');
let nightlyVersion = p.textContent;
let nightlyVersion = p.textContent.match(/\d{4}-\d{1,2}-\d{1,2}/)[0];
window.postMessage({
direction: "rust-search-extension:nightly",
message: {

View file

@ -1,15 +1,21 @@
document.addEventListener("DOMContentLoaded", () => {
let p = document.querySelector('nav.sidebar>div.version>p');
console.log("nightly std...", p.textContent);
let nightlyVersion = p.textContent.match(/\d{4}-\d{1,2}-\d{1,2}/)[0];
chrome.runtime.sendMessage({nightlyVersion, action: "nightly:check"}, response => {
console.log('response:', response);
if (response.state !== 'latest') {
injectScripts(["script/add-nightly-search-index.js"]);
}
});
});
window.addEventListener("message", function(event) {
window.addEventListener("message", function (event) {
if (event.source === window &&
event.data &&
event.data.direction === "rust-search-extension:nightly") {
chrome.runtime.sendMessage({action: "add", ...event.data.message},
chrome.runtime.sendMessage({action: "nightly:add", ...event.data.message},
(response) => {
console.log(response);
}

View file

@ -0,0 +1,27 @@
class NightlySearch extends DocSearch {
constructor(searchIndex) {
super("std", searchIndex);
}
get rootPath() {
return "https://doc.rust-lang.org/nightly/";
}
}
class NightlyDocManager {
static getNightlyVersion() {
return localStorage.getItem('nightly-version') || null;
}
static setNightlyVersion(version) {
localStorage.setItem('nightly-version', version);
}
static getNightlyDocs() {
return JSON.parse(localStorage.getItem('nightly-docs') || "{}");
}
static setNightlyDocs(docs) {
localStorage.setItem('nightly-docs', JSON.stringify(docs));
}
}

View file

@ -7,17 +7,17 @@ local js_files(name, files) = ['%s/%s.js' % [name, file] for file in files];
local json = manifest.new(
name='Rust Search Extension',
version='0.10.0',
version='0.11.0',
keyword='rs',
description='The ultimate search extension for Rust',
)
.addIcons(icons())
.addWebAccessibleResources('script/add-search-index.js')
.addWebAccessibleResources(['script/add-search-index.js', 'script/add-nightly-search-index.js'])
.addBackgroundScripts(
['settings.js', 'deminifier.js']
)
.addBackgroundScripts(js_files('search', ['book', 'crate', 'attribute', 'lint']))
.addBackgroundScripts(js_files('search/docs', ['base', 'std', 'crate-doc']))
.addBackgroundScripts(js_files('search/docs', ['base', 'std', 'nightly', 'crate-doc']))
.addBackgroundScripts(js_files('index', ['attributes', 'books', 'crates', 'std-docs', 'lints', 'labels', 'commands']))
.addBackgroundScripts(js_files('command', ['label', 'help', 'stable']))
.addBackgroundScripts('main.js')