rust-search-extension/extension/script/docs-rs.js

111 lines
4.7 KiB
JavaScript
Raw Normal View History

let [_, crateVersion, crateName] = location.pathname.slice(1).split("/");
// A crate version which added to the extension.
let currentCrateVersion = undefined;
document.addEventListener("DOMContentLoaded", async () => {
let ul = document.querySelector(".landing-search-form-nav>ul");
let childrenNumber = ul.children.length;
if (childrenNumber >= 3) {
2020-04-08 14:53:38 +00:00
await insertFeatureFlagsElement(childrenNumber);
chrome.runtime.sendMessage({crateName, action: "check"}, crate => {
if (crate) {
currentCrateVersion = crate.version;
}
insertAddToExtensionElement();
});
}
});
async function insertFeatureFlagsElement(number) {
let sourceLink = document.querySelector(`.landing-search-form-nav>ul>li:nth-child(${number - 1})>a`);
2020-04-14 11:53:01 +00:00
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]) => {
return `<tr class="module-item">
2020-02-23 09:39:18 +00:00
<td class="docblock-short">
<span class="stab portability"><code style="white-space: nowrap;">${name}</code></span>
</td>
2020-02-14 05:57:16 +00:00
<td>=</td>
<td>${flags}</td>
</tr>`
}).join("");
html = `<table class="feature-flags-table">
<tbody>${tbody}</tbody>
</table>`;
}
sourceLink.parentElement.insertAdjacentHTML("beforebegin",
`<li class="pure-menu-item pure-menu-has-children pure-menu-allow-hover">
<a href="#" class="pure-menu-link" aria-label="Feature flags" aria-haspopup="menu">
<i class="fa fa-fw fa-flag" ></i><span class="title"> Feature flags</span>
</a>
<div class="pure-menu-children feature-flags-content" role="menu">
2020-02-14 05:57:16 +00:00
${html}
</div>
</li>`);
}
2020-02-14 05:57:16 +00:00
function insertAddToExtensionElement() {
let state;
if (currentCrateVersion) {
2020-04-24 11:36:46 +00:00
state = Semver.compareVersion(currentCrateVersion, crateVersion) === -1 ? "outdated" : "latest";
}
2020-03-07 04:52:13 +00:00
// Remove previous element.
let el = document.querySelector(".add-to-extension");
if (el) {
el.remove();
}
let platformElement = document.querySelector(`.landing-search-form-nav>ul>li:last-child`);
let li = document.createElement("li");
2020-03-07 08:12:56 +00:00
li.classList.add("pure-menu-item", "pure-menu-has-children", "pure-menu-allow-hover");
li.onclick = () => {
2020-03-07 04:52:13 +00:00
// Toggle search index added state
if (state === "latest") {
2020-04-08 14:53:38 +00:00
chrome.runtime.sendMessage({crateName, action: "remove"}, response => {
currentCrateVersion = undefined;
insertAddToExtensionElement();
2020-03-07 04:52:13 +00:00
});
} else {
currentCrateVersion = crateVersion;
2020-04-17 10:50:18 +00:00
injectScripts(["script/add-search-index.js"]);
insertAddToExtensionElement();
}
};
2020-03-07 08:12:56 +00:00
let content = `<p>Add this crate to Rust Search Extension then you can search it in the address bar.</p>`;
2020-03-07 04:52:13 +00:00
let iconAttributes = `class="fa fa-fw fa-plus-circle" style="color:#121212"`;
if (state === "latest") {
content = `<p>You already added this crate (v${currentCrateVersion}). Click again to remove it.</p>`;
2020-03-07 04:52:13 +00:00
iconAttributes = `class="fa fa-fw fa-check-circle" style="color:green"`;
} else if (state === "outdated") {
content = `<p>You current version v${currentCrateVersion} is outdated. Click to update to the v${crateVersion}.</p>`;
iconAttributes = `class="fa fa-fw fa-arrow-circle-up" style="color:#e57300"`;
}
li.innerHTML = `<div class="add-to-extension"
2020-03-07 08:12:56 +00:00
title="Add this crate to Rust Search Extension then you can search it in the address bar."
aria-label="Add to Rust Search Extension">
<i ${iconAttributes}></i><span class="title"> to Rust Search Extension</span>
2020-03-07 08:12:56 +00:00
</div>
<div class="pure-menu-children" role="menu">
<div class="add-to-extension-content" onclick="event.stopPropagation()">
${content}
</div>
</div>`;
platformElement.insertAdjacentElement("afterend", li);
}
window.addEventListener("message", function(event) {
if (event.source === window &&
event.data &&
event.data.direction === "rust-search-extension") {
chrome.runtime.sendMessage({action: "add", ...event.data.message},
(response) => {
console.log(response);
}
);
}
});