2020-04-20 14:41:37 +00:00
|
|
|
let [_, crateVersion, crateName] = location.pathname.slice(1).split("/");
|
|
|
|
// A crate version which added to the extension.
|
|
|
|
let currentCrateVersion = undefined;
|
2020-03-06 16:33:48 +00:00
|
|
|
|
2020-02-14 03:31:53 +00:00
|
|
|
document.addEventListener("DOMContentLoaded", async () => {
|
|
|
|
let ul = document.querySelector(".landing-search-form-nav>ul");
|
2020-03-03 14:57:10 +00:00
|
|
|
let childrenNumber = ul.children.length;
|
|
|
|
if (childrenNumber >= 3) {
|
2020-04-08 14:53:38 +00:00
|
|
|
await insertFeatureFlagsElement(childrenNumber);
|
2020-04-20 14:41:37 +00:00
|
|
|
chrome.runtime.sendMessage({crateName, action: "check"}, crate => {
|
|
|
|
if (crate) {
|
|
|
|
currentCrateVersion = crate.version;
|
|
|
|
}
|
|
|
|
|
|
|
|
insertAddToExtensionElement();
|
2020-03-06 16:33:48 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
async function insertFeatureFlagsElement(number) {
|
|
|
|
let sourceLink = document.querySelector(`.landing-search-form-nav>ul>li:nth-child(${number - 1})>a`);
|
2020-02-14 03:31:53 +00:00
|
|
|
|
2020-04-14 11:53:01 +00:00
|
|
|
let response = await fetch(sourceLink.href + "Cargo.toml");
|
|
|
|
let features = await parseCargoFeatures(await response.text());
|
2020-03-06 16:33:48 +00:00
|
|
|
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>`
|
2020-03-06 16:33:48 +00:00
|
|
|
}).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">
|
2020-02-14 03:31:53 +00:00
|
|
|
<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>
|
2020-03-06 16:33:48 +00:00
|
|
|
<div class="pure-menu-children feature-flags-content" role="menu">
|
2020-02-14 05:57:16 +00:00
|
|
|
${html}
|
|
|
|
</div>
|
|
|
|
</li>`);
|
2020-03-06 16:33:48 +00:00
|
|
|
}
|
2020-02-14 05:57:16 +00:00
|
|
|
|
2020-04-20 14:41:37 +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-04-20 14:41:37 +00:00
|
|
|
}
|
|
|
|
|
2020-03-07 04:52:13 +00:00
|
|
|
// Remove previous element.
|
|
|
|
let el = document.querySelector(".add-to-extension");
|
|
|
|
if (el) {
|
|
|
|
el.remove();
|
|
|
|
}
|
|
|
|
|
2020-03-06 16:33:48 +00:00
|
|
|
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");
|
2020-03-06 16:33:48 +00:00
|
|
|
li.onclick = () => {
|
2020-03-07 04:52:13 +00:00
|
|
|
// Toggle search index added state
|
2020-04-20 14:41:37 +00:00
|
|
|
if (state === "latest") {
|
2020-04-08 14:53:38 +00:00
|
|
|
chrome.runtime.sendMessage({crateName, action: "remove"}, response => {
|
2020-04-20 14:41:37 +00:00
|
|
|
currentCrateVersion = undefined;
|
|
|
|
insertAddToExtensionElement();
|
2020-03-07 04:52:13 +00:00
|
|
|
});
|
|
|
|
} else {
|
2020-04-20 14:41:37 +00:00
|
|
|
currentCrateVersion = crateVersion;
|
2020-04-17 10:50:18 +00:00
|
|
|
injectScripts(["script/add-search-index.js"]);
|
2020-04-20 14:41:37 +00:00
|
|
|
insertAddToExtensionElement();
|
2020-03-06 16:33:48 +00:00
|
|
|
}
|
|
|
|
};
|
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"`;
|
2020-04-20 14:41:37 +00:00
|
|
|
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"`;
|
2020-04-20 14:41:37 +00:00
|
|
|
} 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"`;
|
2020-02-14 03:31:53 +00:00
|
|
|
}
|
2020-03-06 16:33:48 +00:00
|
|
|
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."
|
2020-03-06 16:33:48 +00:00
|
|
|
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>
|
2020-03-06 16:33:48 +00:00
|
|
|
</div>`;
|
|
|
|
platformElement.insertAdjacentElement("afterend", li);
|
|
|
|
}
|
|
|
|
|
2020-04-10 15:48:40 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|