/** * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import { html } from "chrome://global/content/vendor/lit.all.mjs"; import { MozLitElement } from "chrome://global/content/lit-utils.mjs"; const lazy = {}; ChromeUtils.defineESModuleGetters(lazy, { BrowserUtils: "resource://gre/modules/BrowserUtils.sys.mjs", }); // TODO put in actual link probably same as labs bug 1951144 const FEEDBACK_LINK = "https://docs.google.com/spreadsheets/d/1hsG7UXGJRN8D4ViaETICDyA0gbBArzmib1qTylmIu8M"; /** * Class representing a link preview element. * * @augments MozLitElement */ class LinkPreviewCard extends MozLitElement { static properties = { generating: { type: Number }, // 0 = off, 1-4 = generating & dots state keyPoints: { type: Array }, pageData: { type: Object }, showWait: { type: Boolean }, }; constructor() { super(); this.keyPoints = []; } addKeyPoint(text) { this.keyPoints.push(text); this.requestUpdate(); } /** * Handles click events on the element. * * @param {MouseEvent} event - The click event. */ handleLink(event) { event.preventDefault(); const url = event.target.href; const win = this.ownerGlobal; const params = { triggeringPrincipal: Services.scriptSecurityManager.createNullPrincipal( {} ), }; // Determine where to open the link based on the event (e.g., new tab, // current tab) const where = lazy.BrowserUtils.whereToOpenLink(event, false, true); win.openLinkIn(url, where, params); this.dispatchEvent(new CustomEvent("LinkPreviewCard:dismiss")); } updated(properties) { if (properties.has("generating")) { if (this.generating > 0) { // Count up to 4 so that we can show 0 to 3 dots. this.dotsTimeout = setTimeout( () => (this.generating = (this.generating % 4) + 1), 500 ); } else { // Setting to false or 0 means we're done generating. clearTimeout(this.dotsTimeout); } } } /** * Renders the link preview element. * * @returns {import('lit').TemplateResult} The rendered HTML template. */ render() { const articleData = this.pageData?.article || {}; const metaData = this.pageData?.metaInfo || {}; const pageUrl = this.pageData?.url || "about:blank"; const siteName = articleData.siteName || ""; const title = metaData["og:title"] || metaData["twitter:title"] || metaData["html:title"] || "This link can’t be previewed"; const description = articleData.excerpt || metaData["og:description"] || metaData["twitter:description"] || metaData.description || "No Reason. Just ’cause. (better error handling incoming)"; let imageUrl = metaData["og:image"] || metaData["twitter:image:src"] || ""; if (!imageUrl.startsWith("https://")) { imageUrl = ""; } const readingTimeMinsFast = articleData.readingTimeMinsFast || ""; const readingTimeMinsSlow = articleData.readingTimeMinsSlow || ""; return html`
${imageUrl ? html` ${title} ` : ""} ${siteName ? html`
${siteName}
` : ""} ${title ? html`

${title}

` : ""} ${description ? html`

${description}

` : ""} ${readingTimeMinsFast && readingTimeMinsSlow ? html`
${readingTimeMinsFast === readingTimeMinsSlow ? `${readingTimeMinsFast} min${readingTimeMinsFast > 1 ? "s" : ""} reading time` : `${readingTimeMinsFast}-${readingTimeMinsSlow} mins reading time`}
` : ""}
${this.generating || this.keyPoints.length ? html`

${this.generating ? "Generating key points" + ".".repeat(this.generating - 1) : "Key points"}


${this.showWait ? html`

This may take a moment the first time you preview a link. Key points should appear more quickly next time.

` : ""}

Key points are AI-generated and may be wrong. Foxfooding feedback

Visit original page

` : ""}
`; } } customElements.define("link-preview-card", LinkPreviewCard);