mirror of
https://github.com/huhu/rust-search-extension
synced 2024-11-14 15:47:12 +00:00
Move common script logic to lib.js
This commit is contained in:
parent
0ff006c6bc
commit
3285d6991b
3 changed files with 35 additions and 37 deletions
|
@ -1,25 +1,5 @@
|
|||
let [_, _crateVersion, crateName] = location.pathname.slice(1).split("/");
|
||||
|
||||
async function parseCargoFeatures(url) {
|
||||
let features = [];
|
||||
let response = await fetch(url);
|
||||
let page = await response.text();
|
||||
let start = page.lastIndexOf("[features]");
|
||||
if (start !== -1) {
|
||||
let lines = page.slice(start + "[features]\n".length).split("\n");
|
||||
for (let line of lines) {
|
||||
if (/.* = \[.*]/g.test(line)) {
|
||||
let [name, flags] = line.split("=");
|
||||
flags = flags.trim().replace(/"/ig, "");
|
||||
features.push([name, flags]);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return features;
|
||||
}
|
||||
|
||||
document.addEventListener("DOMContentLoaded", async () => {
|
||||
let ul = document.querySelector(".landing-search-form-nav>ul");
|
||||
let childrenNumber = ul.children.length;
|
||||
|
@ -34,7 +14,8 @@ document.addEventListener("DOMContentLoaded", async () => {
|
|||
async function insertFeatureFlagsElement(number) {
|
||||
let sourceLink = document.querySelector(`.landing-search-form-nav>ul>li:nth-child(${number - 1})>a`);
|
||||
|
||||
let features = await parseCargoFeatures(sourceLink.href + "Cargo.toml");
|
||||
let response = await fetch(sourceLink.href + "Cargo.toml");
|
||||
let features = await parseCargoFeatures(await response.text());
|
||||
let html = `<div style="padding: 1rem"><p>This crate has no feature flag.</p></div>`;
|
||||
if (features.length > 0) {
|
||||
let tbody = features.map(([name, flags]) => {
|
||||
|
@ -101,21 +82,6 @@ function insertAddToExtensionElement(added) {
|
|||
platformElement.insertAdjacentElement("afterend", li);
|
||||
}
|
||||
|
||||
function injectScripts(paths) {
|
||||
paths.map(path => {
|
||||
let script = document.createElement("script");
|
||||
script.src = chrome.runtime.getURL(path);
|
||||
script.onload = () => {
|
||||
// Remove self after loaded
|
||||
script.remove();
|
||||
};
|
||||
return script;
|
||||
}).forEach(script => {
|
||||
document.body.insertAdjacentElement('beforeBegin', script);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
window.addEventListener("message", function(event) {
|
||||
if (event.source === window &&
|
||||
event.data &&
|
||||
|
|
32
extension/script/lib.js
Normal file
32
extension/script/lib.js
Normal file
|
@ -0,0 +1,32 @@
|
|||
async function parseCargoFeatures(content) {
|
||||
let features = [];
|
||||
|
||||
let start = content.lastIndexOf("[features]");
|
||||
if (start !== -1) {
|
||||
let lines = content.slice(start + "[features]\n".length).split("\n");
|
||||
for (let line of lines) {
|
||||
if (/.* = \[.*]/g.test(line)) {
|
||||
let [name, flags] = line.split("=");
|
||||
flags = flags.trim().replace(/"/ig, "");
|
||||
features.push([name.trim(), flags]);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return features;
|
||||
}
|
||||
|
||||
function injectScripts(paths) {
|
||||
paths.map(path => {
|
||||
let script = document.createElement("script");
|
||||
script.src = chrome.runtime.getURL(path);
|
||||
script.onload = () => {
|
||||
// Remove self after loaded
|
||||
script.remove();
|
||||
};
|
||||
return script;
|
||||
}).forEach(script => {
|
||||
document.body.insertAdjacentElement('beforeBegin', script);
|
||||
});
|
||||
}
|
|
@ -24,7 +24,7 @@ local manifest = {
|
|||
matches: [
|
||||
"*://docs.rs/*"
|
||||
],
|
||||
js: js_files("script", ["docs-rs"]),
|
||||
js: js_files("script", ["lib", "docs-rs"]),
|
||||
css: ["script/docs-rs.css"],
|
||||
run_at: "document_start"
|
||||
}],
|
||||
|
|
Loading…
Reference in a new issue