FilterAdult.sys.mjs (1710B)
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 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs"; 6 import { FilterAdultComponent } from "moz-src:///toolkit/components/uniffi-bindgen-gecko-js/components/generated/RustFilterAdult.sys.mjs"; 7 8 const lazy = {}; 9 10 XPCOMUtils.defineLazyPreferenceGetter( 11 lazy, 12 "gFilterAdultEnabled", 13 "browser.newtabpage.activity-stream.filterAdult", 14 true 15 ); 16 17 export class _FilterAdult { 18 #comp = null; 19 20 constructor() { 21 this.#comp = FilterAdultComponent.init(); 22 } 23 24 /** 25 * Filter out any link objects that have a url with an adult base domain. 26 * 27 * @param {string[]} links 28 * An array of links to test. 29 * @returns {string[]} 30 * A filtered array without adult links. 31 */ 32 filter(links) { 33 if (!lazy.gFilterAdultEnabled) { 34 return links; 35 } 36 37 return links.filter(({ url }) => { 38 try { 39 const uri = Services.io.newURI(url); 40 return !this.#comp.contains(Services.eTLD.getBaseDomain(uri)); 41 } catch (ex) { 42 return true; 43 } 44 }); 45 } 46 47 /** 48 * Determine if the supplied url is an adult url or not. 49 * 50 * @param {string} url 51 * The url to test. 52 * @returns {boolean} 53 * True if it is an adult url. 54 */ 55 isAdultUrl(url) { 56 if (!lazy.gFilterAdultEnabled) { 57 return false; 58 } 59 try { 60 const uri = Services.io.newURI(url); 61 return this.#comp.contains(Services.eTLD.getBaseDomain(uri)); 62 } catch (ex) { 63 return false; 64 } 65 } 66 } 67 68 export const FilterAdult = new _FilterAdult();