PageAssistChild.sys.mjs (1995B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 const lazy = {}; 6 ChromeUtils.defineESModuleGetters(lazy, { 7 ReaderMode: "moz-src:///toolkit/components/reader/ReaderMode.sys.mjs", 8 Readerable: "resource://gre/modules/Readerable.sys.mjs", 9 }); 10 11 /** 12 * Represents a child actor for getting page data from the browser. 13 */ 14 export class PageAssistChild extends JSWindowActorChild { 15 async receiveMessage(message) { 16 switch (message.name) { 17 case "PageAssist:FetchPageData": 18 return this.getPageData(); 19 } 20 21 //expected a return value. consistent-return (eslint) 22 return undefined; 23 } 24 25 /** 26 * Fetches and returns page data including URL, title, content, text content, excerpt, and reader mode status. 27 * 28 * @returns {Promise<{ 29 * url: string, 30 * title: string, 31 * content: string, 32 * textContent: string, 33 * excerpt: string, 34 * isReaderable: boolean 35 * } | null>} 36 */ 37 async getPageData() { 38 try { 39 const doc = this.contentWindow.document; 40 if ( 41 !lazy.Readerable.shouldCheckUri(doc.documentURIObject) || 42 !lazy.Readerable.isProbablyReaderable(doc) 43 ) { 44 return { 45 url: this.contentWindow.location.href, 46 title: doc.title || "", 47 content: "", 48 textContent: "", 49 excerpt: "", 50 isReaderable: false, 51 }; 52 } 53 54 const article = await lazy.ReaderMode.parseDocument(doc); 55 56 return { 57 url: this.contentWindow.location.href, 58 title: article?.title || doc.title || "", 59 content: article?.content || "", 60 textContent: article?.textContent || doc.body?.innerText || "", 61 excerpt: article?.excerpt || "", 62 isReaderable: !!article, 63 }; 64 } catch (e) { 65 console.error("Error fetching page data:", e); 66 return null; 67 } 68 } 69 }