tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CanonicalURLChild.sys.mjs (1544B)


      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 
      7 ChromeUtils.defineESModuleGetters(lazy, {
      8  findCandidates: "moz-src:///browser/components/tabnotes/CanonicalURL.sys.mjs",
      9  pickCanonicalUrl:
     10    "moz-src:///browser/components/tabnotes/CanonicalURL.sys.mjs",
     11 });
     12 
     13 /**
     14 * Identifies the canonical URL in a top-level content frame, if possible,
     15 * and notifies the parent process about it.
     16 */
     17 export class CanonicalURLChild extends JSWindowActorChild {
     18  /**
     19   * @param {Event} event
     20   */
     21  handleEvent(event) {
     22    switch (event.type) {
     23      case "DOMContentLoaded":
     24      case "pageshow":
     25        this.#discoverCanonicalUrl();
     26    }
     27  }
     28 
     29  /**
     30   * Called when a message is received from the parent process.
     31   *
     32   * @param {ReceiveMessageArgument} msg
     33   */
     34  receiveMessage(msg) {
     35    switch (msg.name) {
     36      case "CanonicalURL:Detect":
     37        this.#discoverCanonicalUrl();
     38        break;
     39    }
     40  }
     41 
     42  /**
     43   * Find a canonical URL in the document and tell the parent about it.
     44   */
     45  #discoverCanonicalUrl() {
     46    const candidates = lazy.findCandidates(this.document);
     47    const canonicalUrl = lazy.pickCanonicalUrl(candidates);
     48    const canonicalUrlSources = Object.keys(candidates).filter(
     49      candidate => candidates[candidate]
     50    );
     51    this.sendAsyncMessage("CanonicalURL:Identified", {
     52      canonicalUrl,
     53      canonicalUrlSources,
     54    });
     55  }
     56 }