diff --git a/extension/app.js b/extension/app.js index 0043332..a70a6d3 100644 --- a/extension/app.js +++ b/extension/app.js @@ -13,14 +13,11 @@ c.browser.runtime.setUninstallURL( c.browser.runtime.onMessage.addListener((request, sender, sendResponse) => { switch (request.action) { case "check": { - let crateName = request.crateName; - let index = localStorage.getItem(`${crateName}`); - sendResponse({added: !!index}); + sendResponse({added: !!CrateDocSearchManager.checkCrate(request.crateName)}); break; } case "remove": { - let crateName = request.crateName; - let index = localStorage.removeItem(`${crateName}`); + CrateDocSearchManager.removeCrate(request.crateName); sendResponse(true); break; } @@ -29,8 +26,7 @@ c.browser.runtime.onMessage.addListener((request, sender, sendResponse) => { }); c.browser.runtime.onMessageExternal.addListener((request, sender, sendResponse) => { - let crateName = request.crateName; - localStorage.setItem(crateName, JSON.stringify(request.searchIndex[crateName])); + CrateDocSearchManager.addCrate(request.crateName, request.crateVersion, request.searchIndex); console.log(request); sendResponse("ok"); return true; diff --git a/extension/script/crate-docs.js b/extension/script/crate-docs.js index e9522f1..f14c248 100644 --- a/extension/script/crate-docs.js +++ b/extension/script/crate-docs.js @@ -1,8 +1,14 @@ -new Compat().browser.runtime.sendMessage("ennpfpdlaclocpomkiablnmbppdnlhoh", { - crateName: location.pathname.match(/[0-9a-z_-]+/i)[0], - searchIndex: window.searchIndex - }, - (response) => { - console.log(response); - } -); \ No newline at end of file +(function() { + // Parse crate info from location pathname. + let [_, crateName, crateVersion, ...others] = location.pathname.split("/"); + + new Compat().browser.runtime.sendMessage("ennpfpdlaclocpomkiablnmbppdnlhoh", { + crateName, + crateVersion, + searchIndex: window.searchIndex, + }, + (response) => { + console.log(response); + } + ); +})(); \ No newline at end of file diff --git a/extension/search/docs/crate-doc.js b/extension/search/docs/crate-doc.js new file mode 100644 index 0000000..b1ba734 --- /dev/null +++ b/extension/search/docs/crate-doc.js @@ -0,0 +1,70 @@ +class CrateDocSearch extends DocSearch { + constructor({name, version, searchIndex}) { + super(searchIndex, `https://docs.rs/${name}/${version}/`); + } +} + +class CrateDocSearchManager { + constructor() { + this.cachedCrate = null; + this.cachedCrateSearcher = null; + } + + search(query) { + query = query.replace("@", "").trim(); + let [crateName, keyword] = query.split(" "); + + let searcher = null; + if (this.cachedCrate === crateName) { + searcher = this.cachedCrateSearcher; + } else { + let crate = CrateDocSearchManager.getCrate(crateName); + if (crate) { + searcher = new CrateDocSearch(crate); + this.cachedCrate = crate; + this.cachedCrateSearcher = searcher; + return searcher.search(keyword); + } else { + return []; + } + } + + return searcher.search(keyword); + } + + static getCrates() { + return JSON.parse(localStorage.getItem("crates") || "[]"); + } + + static checkCrate(name) { + let crates = CrateDocSearchManager.getCrates(); + return crates.find(item => item.name === name); + } + + static getCrate(name) { + let crate = CrateDocSearchManager.checkCrate(name); + if (crate) { + crate["searchIndex"] = JSON.parse(localStorage.getItem(`${name}`)); + return crate; + } else { + return null; + } + } + + static addCrate(name, version, searchIndex) { + localStorage.setItem(name, JSON.stringify(searchIndex)); + let crates = CrateDocSearchManager.getCrates(); + crates.push({name, version}); + localStorage.setItem("crates", JSON.stringify(crates)); + } + + static removeCrate(name) { + let crates = CrateDocSearchManager.getCrates(); + let index = crates.findIndex(item => item.name === name); + if (index > -1) { + crates.splice(index); + } + localStorage.setItem("crates", JSON.stringify(crates)); + localStorage.removeItem(`${name}`); + } +} \ No newline at end of file diff --git a/manifest.jsonnet b/manifest.jsonnet index d713d7b..3167d65 100644 --- a/manifest.jsonnet +++ b/manifest.jsonnet @@ -36,7 +36,7 @@ local manifest = { background: { scripts: ["compat.js", "settings.js", "deminifier.js",] + js_files("search" ,["book", "crate", "attribute", "lint"]) + - js_files("search/docs" ,["base", "std",]) + + js_files("search/docs" ,["base", "std", "crate-doc"]) + js_files("index" ,["books", "crates", "std-docs", "lints"]) + js_files("command" ,["base", "history", "manager"]) + ["omnibox.js", "main.js","app.js",]