UNPKG

webextension-store-meta

Version:

Get browser extension(webextension) item meta from Chrome Web Store and Firefox add-ons.

159 lines 5.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Amo = void 0; const node_querystring_1 = require("node:querystring"); const domhandler_1 = require("domhandler"); const Parser_1 = require("htmlparser2/lib/Parser"); const fetch_text_1 = require("../utils/fetch-text"); const SourceDOM_1 = require("./SourceDOM"); const SourceJSONLD_1 = require("./SourceJSONLD"); const SourceOG_1 = require("./SourceOG"); const SourceReduxStoreState_1 = require("./SourceReduxStoreState"); class Amo { config; constructor(config) { this.config = config || {}; } static async load(config) { const instance = new Amo(config); await instance.load(); return instance; } async load() { let qs = this.config.qs ? typeof this.config.qs === "string" ? this.config.qs : (0, node_querystring_1.stringify)(this.config.qs) : ""; if (qs && !qs.startsWith("?")) { qs = `?${qs}`; } const locale = this.config.locale ? `${this.config.locale}/` : ""; const url = `https://addons.mozilla.org/${locale}firefox/addon/${this.config.id}${qs}`; const html = await (0, fetch_text_1.fetchText)(url, this.config.options); const handler = new domhandler_1.DomHandler(); new Parser_1.Parser(handler).end(html); this._dom = handler.dom; return this; } meta() { return { name: this.name(), description: this.description(), ratingValue: this.ratingValue(), ratingCount: this.ratingCount(), users: this.users(), price: this.price(), priceCurrency: this.priceCurrency(), version: this.version(), url: this.url(), image: this.image(), operatingSystem: this.operatingSystem(), size: this.size(), lastUpdated: this.lastUpdated(), }; } name() { return (this.sourceReduxStoreState.name() || this.sourceJSONLD.name() || this.sourceDOM.name()); } description() { return (this.sourceReduxStoreState.description() || this.sourceJSONLD.description() || this.sourceOG.description() || this.sourceDOM.description()); } ratingValue() { return (this.sourceReduxStoreState.ratingValue() || this.sourceJSONLD.ratingValue() || this.sourceDOM.ratingValue()); } ratingCount() { return (this.sourceReduxStoreState.ratingCount() || this.sourceJSONLD.ratingCount() || this.sourceDOM.ratingCount()); } users() { return this.sourceReduxStoreState.users() || this.sourceDOM.users(); } price() { return this.sourceJSONLD.price(); } priceCurrency() { return this.sourceJSONLD.priceCurrency(); } version() { return (this.sourceReduxStoreState.version() || this.sourceJSONLD.version() || this.sourceDOM.version()); } url() { return (this.sourceReduxStoreState.url() || this.sourceJSONLD.url() || this.sourceOG.url() || this.sourceDOM.url()); } image() { return (this.sourceReduxStoreState.image() || this.sourceJSONLD.image() || this.sourceOG.image() || this.sourceDOM.image()); } operatingSystem() { return this.sourceJSONLD.operatingSystem(); } size() { return this.sourceReduxStoreState.size() || this.sourceDOM.size(); } lastUpdated() { return (this.sourceReduxStoreState.lastUpdated() || this.sourceDOM.lastUpdated()); } /** @internal */ _dom; get dom() { if (!this._dom) { throw new Error("Item not loaded. Please run `await instance.load()` first.`"); } return this._dom; } /** @internal */ _sourceDOM; /** @internal */ get sourceDOM() { if (!this._sourceDOM) { this._sourceDOM = new SourceDOM_1.SourceDOM(this.dom); } return this._sourceDOM; } /** @internal */ _sourceJSONLD; /** @internal */ get sourceJSONLD() { if (!this._sourceJSONLD) { this._sourceJSONLD = new SourceJSONLD_1.SourceJSONLD(this.dom); } return this._sourceJSONLD; } /** @internal */ _sourceOG; /** @internal */ get sourceOG() { if (!this._sourceOG) { this._sourceOG = new SourceOG_1.SourceOG(this.dom); } return this._sourceOG; } /** @internal */ _sourceReduxStoreState; /** @internal */ get sourceReduxStoreState() { if (!this._sourceReduxStoreState) { this._sourceReduxStoreState = new SourceReduxStoreState_1.SourceReduxStoreState(this.dom); } return this._sourceReduxStoreState; } } exports.Amo = Amo; exports.default = Amo; //# sourceMappingURL=index.js.map