mirror of
https://github.com/huhu/rust-search-extension
synced 2024-11-14 15:47:12 +00:00
Add CrateDocSearcher and manager, crate docs change to immediately invoke function
This commit is contained in:
parent
674428441f
commit
cee11ef52a
4 changed files with 88 additions and 16 deletions
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
);
|
||||
(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);
|
||||
}
|
||||
);
|
||||
})();
|
70
extension/search/docs/crate-doc.js
Normal file
70
extension/search/docs/crate-doc.js
Normal file
|
@ -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}`);
|
||||
}
|
||||
}
|
|
@ -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",]
|
||||
|
|
Loading…
Reference in a new issue