Bundle content scripts

This commit is contained in:
Folyd 2024-05-27 00:55:55 -07:00
parent a1f06ea5ea
commit c0a377bfad
10 changed files with 42 additions and 27 deletions

View file

@ -1,7 +1,7 @@
include core/extension.mk
include build.mk
.PHONY: chrome manage
.PHONY: chrome manage bundle
# Override the included `assert` target.
assert:
@ -11,5 +11,9 @@ assert:
manage:
@cd manage && cargo run
bundle:
@echo "Building extension/content_script_bundle.js..."
@esbuild content-script-bundle.js --bundle --minify --global-name=rse --target=es2015 --outdir=extension
# Build macro-railroad wasm module and js
macro-railroad: extension/wasm/macro-railroad.wasm extension/script/macro-railroad-wasm.js

12
content-script-bundle.js Normal file
View file

@ -0,0 +1,12 @@
// esbuild content-script-bundle.js --bundle --minify --global-name=rse --target=es2015 --outdir=extension
import storage from "./extension/core/storage.js";
import settings from "./extension/settings.js";
import IndexSetter from "./extension/index-setter.js";
import CrateDocManager from "./extension/crate-manager.js";
export {
storage,
settings,
IndexSetter,
CrateDocManager
}

View file

@ -0,0 +1 @@
var rse=(()=>{var g=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var p=Object.getOwnPropertyNames;var w=Object.prototype.hasOwnProperty;var h=(t,e)=>{for(var s in e)g(t,s,{get:e[s],enumerable:!0})},y=(t,e,s,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of p(e))!w.call(t,r)&&r!==s&&g(t,r,{get:()=>e[r],enumerable:!(i=x(e,r))||i.enumerable});return t};var C=t=>y(g({},"__esModule",{value:!0}),t);var n=(t,e,s)=>new Promise((i,r)=>{var d=c=>{try{o(s.next(c))}catch(u){r(u)}},l=c=>{try{o(s.throw(c))}catch(u){r(u)}},o=c=>c.done?i(c.value):Promise.resolve(c.value).then(d,l);o((s=s.apply(t,e)).next())});var v={};h(v,{CrateDocManager:()=>I,IndexSetter:()=>m,settings:()=>f,storage:()=>a});var S={getAllItems:()=>new Promise(t=>{chrome.storage.local.get(null,t)}),getItem:t=>new Promise(e=>{chrome.storage.local.get(t,s=>{e(s?s[t]:null)})}),setItem:(t,e)=>new Promise(s=>{chrome.storage.local.set({[t]:e},s)}),removeItem:t=>new Promise(e=>{chrome.storage.local.remove(t,e)})};var a=S;var R={get autoUpdate(){return n(this,null,function*(){return(yield a.getItem("auto-update"))||!1})},set autoUpdate(t){a.setItem("auto-update",t)},get isOfflineMode(){return n(this,null,function*(){return(yield a.getItem("offline-mode"))||!1})},set isOfflineMode(t){a.setItem("offline-mode",t)},get offlineDocPath(){return n(this,null,function*(){return(yield a.getItem("offline-path"))||""})},set offlineDocPath(t){a.setItem("offline-path",t)},get crateRegistry(){return n(this,null,function*(){return(yield a.getItem("crate-registry"))||"crates.io"})},set crateRegistry(t){a.setItem("crate-registry",t)},get defaultSearch(){return n(this,null,function*(){return(yield a.getItem("default-search"))||{thirdPartyDocs:!1,docsRs:!0,attributes:!0}})},set defaultSearch(t){a.setItem("default-search",t)},get showMacroRailroad(){return n(this,null,function*(){let t=yield a.getItem("show-macro-railroad");return t===void 0?!0:t})},set showMacroRailroad(t){a.setItem("show-macro-railroad",t)},get keepCratesUpToDate(){return n(this,null,function*(){let t=yield a.getItem("keep-crates-up-to-date");return t===void 0?!1:t})},set keepCratesUpToDate(t){a.setItem("keep-crates-up-to-date",t)}},f=R;var m=class t{static setStdStableIndex(e){a.setItem("index-std-stable",e)}static setStdNightlyIndex(e){a.setItem("index-std-nightly",e)}static setBookIndex(e){a.setItem("index-book",e)}static setLabelIndex(e){a.setItem("index-label",e)}static setRfcIndex(e){a.setItem("index-rfc",e)}static setCrateMapping(e){a.setItem("index-crate-mapping",e)}static setCrateIndex(e){a.setItem("index-crate",e)}static setLintIndex(e){a.setItem("index-lint",e)}static setCaniuseIndex(e){a.setItem("index-caniuse",e)}static setRustcIndex(e){a.setItem("index-rustc",e)}static setTargetIndex(e){a.setItem("index-target",e)}static setCommandIndex(e){a.setItem("index-command",e)}static updateAllIndex(){t.setBookIndex(booksIndex),t.setCaniuseIndex(caniuseIndex),t.setCommandIndex(commandsIndex),t.setCrateIndex(crateIndex),t.setCrateMapping(mapping),t.setLabelIndex(labelsIndex),t.setLintIndex(lintsIndex),t.setRfcIndex(rfcsIndex),t.setRustcIndex(rustcIndex),t.setStdStableIndex(searchIndex),t.setTargetIndex(targetsIndex)}};var I=class t{static getCrates(){return n(this,null,function*(){return(yield a.getItem("crates"))||{}})}static getCrateByName(e){return n(this,null,function*(){let s=yield t.getCrates();if(s[e])return s[e];{let i=Object.entries(s).find(([r,{crateName:d}])=>d==e);return i?i[1]:null}})}static getCrateSearchIndex(e){return n(this,null,function*(){let s=yield a.getItem(`@${e}`);if(s)return s;{let i=yield t.getCrates(),r=Object.entries(i).find(([d,{crateName:l}])=>l==e);if(r){let d=r[0];return yield a.getItem(`@${d}`)}else return null}})}static addCrate(d){return n(this,arguments,function*({libName:e,crateVersion:s,searchIndex:i,crateName:r}){if(i&&e in i){yield a.setItem(`@${e}`,i);let l=i[e].doc,o=yield t.getCrates();e in o?o[e]={version:s,doc:l,time:o[e].time,crateName:r}:o[e]={version:s,doc:l,time:Date.now(),crateName:r},yield a.setItem("crates",o)}})}static removeCrate(e){return n(this,null,function*(){let s=yield t.getCrates();delete s[e],yield a.setItem("crates",s),yield a.removeItem(`@${e}`)})}};return C(v);})();

View file

@ -24,7 +24,8 @@ import CommandManager from "./core/command/manager.js";
import CrateDocManager from "./crate-manager.js";
import { Omnibox, c } from "./core/index.js";
const INDEX_UPDATE_URL = "https://rust.extension.sh/update";
const RUST_RELEASE_README_URL = "https://github.com/rust-lang/rust/blob/master/RELEASES.md";
const manifest = chrome.runtime.getManifest();
// Get the information about the current platform os.
@ -46,9 +47,6 @@ async function start(el) {
let crateRegistry = await settings.crateRegistry;
const os = await getPlatformOs();
const RUST_RELEASE_README_URL = "https://github.com/rust-lang/rust/blob/master/RELEASES.md";
const INDEX_UPDATE_URL = "https://rust.extension.sh/update";
const crateSearcher = new CrateSearch(await IndexManager.getCrateMapping(), await IndexManager.getCrateIndex());
let caniuseSearcher = new CaniuseSearch(await IndexManager.getCaniuseIndex());
let bookSearcher = new BookSearch(await IndexManager.getBookIndex());

View file

@ -31,9 +31,9 @@ window.addEventListener("message", function (event) {
event.data.direction === "rust-search-extension:std") {
let searchIndex = event.data.message.searchIndex;
if (TARGET === 'stable') {
IndexManager.setStdStableIndex(searchIndex);
rse.IndexSetter.setStdStableIndex(searchIndex);
} else {
IndexManager.setStdNightlyIndex(searchIndex);
rse.IndexSetter.setStdNightlyIndex(searchIndex);
}
let now = new Date();
let version = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`;

View file

@ -95,7 +95,7 @@ document.addEventListener("DOMContentLoaded", async () => {
// Exclude /crate/** pages
if (menus.children.length >= 3 && !location.pathname.includes("/crate/")) {
// Query installed crates from chrome.storage API
let crates = await storage.getItem("crates") || {};
let crates = await rse.storage.getItem("crates") || {};
let installedCrate = crates[crateName];
if (!installedCrate && crates[libName]) {
installedCrate = crates[libName];
@ -149,7 +149,7 @@ document.addEventListener("DOMContentLoaded", async () => {
rseButton.parentElement.insertAdjacentElement("beforebegin", advisoryMenu);
}
if (getState(installedVersion) === "outdated" && await settings.keepCratesUpToDate) {
if (getState(installedVersion) === "outdated" && await rse.settings.keepCratesUpToDate) {
// Auto update outdated crates if the user has enabled the setting.
injectScripts(["script/lib.js", "script/add-search-index.js"]);
}
@ -275,7 +275,7 @@ function insertAddToExtensionElement(state) {
// Toggle search index added state
if (state === "latest") {
// Use the libName to remove the installed crate.
await CrateDocManager.removeCrate(libName);
await rse.CrateDocManager.removeCrate(libName);
insertAddToExtensionElement(getState(undefined));
} else {
injectScripts(["script/lib.js", "script/add-search-index.js"]);
@ -352,7 +352,7 @@ window.addEventListener("message", async function (event) {
event.data &&
event.data.direction === "rust-search-extension:docs.rs") {
let message = event.data.message;
await CrateDocManager.addCrate(message);
await rse.CrateDocManager.addCrate(message);
insertAddToExtensionElement(getState(message.crateVersion));
console.log("Congrats! This crate has been installed successfully!");
}

View file

@ -8,7 +8,7 @@ async function load() {
let macros = document.querySelectorAll('.item-decl');
if (!macros || macros.length === 0) return;
if (!await settings.showMacroRailroad) return;
if (!await rse.settings.showMacroRailroad) return;
await wasm_bindgen(chrome.runtime.getURL('wasm/macro-railroad.wasm'));
injectCss();

View file

@ -6,40 +6,40 @@ window.addEventListener("message", async function (event) {
console.log('target:', message.target);
switch (message.target) {
case 'book': {
IndexManager.setBookIndex(message.index);
rse.IndexSetter.setBookIndex(message.index);
break;
}
case 'caniuse': {
IndexManager.setCaniuseIndex(message.index);
rse.IndexSetter.setCaniuseIndex(message.index);
break;
}
case 'command': {
IndexManager.setCommandIndex(message.index);
rse.IndexSetter.setCommandIndex(message.index);
break;
}
case 'crate': {
IndexManager.setCrateIndex(message.index);
IndexManager.setCrateMapping(message.mapping);
rse.IndexSetter.setCrateIndex(message.index);
rse.IndexSetter.setCrateMapping(message.mapping);
break;
}
case 'label': {
IndexManager.setLabelIndex(message.index);
rse.IndexSetter.setLabelIndex(message.index);
break;
}
case 'lint': {
IndexManager.setLintIndex(message.index);
rse.IndexSetter.setLintIndex(message.index);
break;
}
case 'rfc': {
IndexManager.setRfcIndex(message.index);
rse.IndexSetter.setRfcIndex(message.index);
break;
}
case 'rustc': {
IndexManager.setRustcIndex(message.index);
rse.IndexSetter.setRustcIndex(message.index);
break;
}
case 'target': {
IndexManager.setTargetIndex(message.index);
rse.IndexSetter.setTargetIndex(message.index);
break;
}
}

View file

@ -1,6 +1,7 @@
import { c } from "../../core/index.js";
import DocSearch from "./base.js";
import CrateDocManager from "../../crate-manager.js";
import settings from "../../settings.js";
// A DocSearch dedicated to a single crate based on the search-index.
class SingleCrateDocSearch extends DocSearch {

View file

@ -46,18 +46,17 @@ else
.addPermissions(host_permissions)
;
local INDEX_MANAGER_FILES = ['core/storage.js', 'index-manager.js'];
json.addIcons(icons())
.addPermissions(['storage', 'unlimitedStorage'])
.setOptionsUi('manage/index.html')
.addContentScript(
matches=['*://docs.rs/*'],
js=['core/storage.js', 'crate-manager.js'] + utils.js_files('script', ['lib', 'docs-rs', 'svgs', 'rust-src-navigate', 'semver']),
js=['content-script-bundle.js'] + utils.js_files('script', ['lib', 'docs-rs', 'svgs', 'rust-src-navigate', 'semver']),
css=['script/docs-rs.css', 'script/details-toggle.css'],
)
.addContentScript(
matches=['*://doc.rust-lang.org/*'],
js=INDEX_MANAGER_FILES + utils.js_files('script', ['lib', 'doc-rust-lang-org', 'rust-src-navigate']),
js=['content-script-bundle.js'] + utils.js_files('script', ['lib', 'doc-rust-lang-org', 'rust-src-navigate']),
css=['script/doc-rust-lang-org.css', 'script/details-toggle.css'],
exclude_matches=['*://doc.rust-lang.org/nightly/nightly-rustc/*'],
)
@ -68,13 +67,13 @@ json.addIcons(icons())
)
.addContentScript(
matches=['*://rust.extension.sh/update'],
js=INDEX_MANAGER_FILES + utils.js_files('script', ['rust-extension-sh']),
js=['content-script-bundle.js'] + utils.js_files('script', ['rust-extension-sh']),
css=[],
).addContentScript(
matches=[
'*://docs.rs/*',
'*://doc.rust-lang.org/*',
],
js=['settings.js'] + utils.js_files('script', ['lib', 'macro-railroad', 'macro-railroad-wasm']),
js=['content-script-bundle.js'] + utils.js_files('script', ['lib', 'macro-railroad', 'macro-railroad-wasm']),
css=['script/macro-railroad.css'],
)