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) { 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`); let response = await fetch(sourceLink.href + "Cargo.toml"); let features = await parseCargoFeatures(await response.text()); let html = `

This crate has no feature flag.

`; if (features.length > 0) { let tbody = features.map(([name, flags]) => { return ` ${name} = ${flags} ` }).join(""); html = `${tbody}
`; } sourceLink.parentElement.insertAdjacentHTML("beforebegin", `
  • Feature flags
  • `); } function insertAddToExtensionElement() { let state; if (currentCrateVersion) { state = Semver.compareVersion(currentCrateVersion, crateVersion) === -1 ? "outdated" : "latest"; } // 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"); li.classList.add("pure-menu-item", "pure-menu-has-children", "pure-menu-allow-hover"); li.onclick = () => { // Toggle search index added state if (state === "latest") { chrome.runtime.sendMessage({crateName, action: "remove"}, response => { currentCrateVersion = undefined; insertAddToExtensionElement(); }); } else { currentCrateVersion = crateVersion; injectScripts(["script/add-search-index.js"]); insertAddToExtensionElement(); } }; let content = `

    Add this crate to Rust Search Extension then you can search it in the address bar.

    `; let iconAttributes = `class="fa fa-fw fa-plus-circle" style="color:#121212"`; if (state === "latest") { content = `

    You already added this crate (v${currentCrateVersion}). Click again to remove it.

    `; iconAttributes = `class="fa fa-fw fa-check-circle" style="color:green"`; } else if (state === "outdated") { content = `

    You current version v${currentCrateVersion} is outdated. Click to update to the v${crateVersion}.

    `; iconAttributes = `class="fa fa-fw fa-arrow-circle-up" style="color:#e57300"`; } li.innerHTML = `
    to Rust Search Extension
    `; 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); } ); } });