PageSourceSearch

https://www.grefrath.de/modules/contrib/cookies_google_translator/js/cookies_google_translator.js?v=10.6.18

js grefrath.de collected 2026-10-02 06:55:51 UTC 3,299 bytes, 91 lines download raw bytes

1/**
2 * @file
3 * Defines Javascript behaviors for the cookies module.
4 * These are invoked once the user performs a cookie consent
5 * action like: accept / deny / revoke to immediately
6 * react on that with loading a script, video, iframe, etc.
7 * or if consent is revoked, removing existing cookies,
8 * stop tracking or similar.
9 *
10 * This is NOT invoked on each page load, only immediately, when the user
11 * takes concrete action in the COOKiES banner.
12 */
13
14(function (Drupal) {
15  "use strict";
16
17  /**
18   * Define defaults.
19   */
20  Drupal.behaviors.cookies_google_translator = {
21    // id corresponding to the cookies_service.schema->id.
22    id: "google_translator",
23    blockSelector: "div[data-block-plugin-id=\"google_translator\"]",
24    scriptSelector: "script[src*=\"//translate.google\"]",
25
26    /**
27     * Called when consent was given.
28     */
29    consentGiven: function (context, config) {
30      // Iterate over all blocks based on the selector
31      document.querySelectorAll(this.blockSelector)
32        .forEach((block) => {
33          // Reset the display style to show the block
34          block.style.display = "";
35          // Trigger the disclaimer link in the implementation
36          // from the main module to load external data
37          block.querySelector(config.selector)?.click();
38        });
39    },
40
41    /**
42     * Called when consent was denied / revoked.
43     */
44    consentDenied: function (context, config) {
45      /**
46       * Get an array of all cookie names. Can optionally be filtered by the parameter 'names'.
47       * @param {string[]} names If provided, only check for these cookies.
48       * @returns {string[]} Array with cookie names.
49       */
50      const getCookieNames = (names = []) => decodeURIComponent(document.cookie).split(";")
51        .map(cookie => cookie.trimStart().split("=", 1).toString())
52        .filter(cookieName => names.length > 0 ? names.includes(cookieName) : true);
53      let needReload = false;
54      // Hide the block
55      document.querySelectorAll(this.blockSelector)
56        .forEach((block) => block.style.display = "none");
57      // Remove cookies
58      const cookieNames = ["googtrans"];
59      getCookieNames(cookieNames).forEach((name) => {
60        document.cookie = `${name}=;domain=${location.hostname};path=/;max-age=0;`;
61        needReload = true;
62      });
63      // Check script types if there were no cookies to delete
64      if (!needReload &&
65        document.querySelector(
66          `${this.scriptSelector}:not([type]),${this.scriptSelector}[type="text/javascript"]`,
67        ) != null) {
68        needReload = true;
69      }
70      // Reload page
71      if (needReload) {
72        location.reload();
73      }
74    },
75
76    attach: function (context, settings) {
77      const self = this;
78      const config = settings.googleTranslatorDisclaimer || {};
79      document.addEventListener("cookiesjsrUserConsent", function (event) {
80        const service = (typeof event.detail.services === "object") ? event.detail.services : {};
81        if (typeof service[self.id] !== "undefined" && service[self.id]) {
82          // Consent was given:
83          self.consentGiven(context, config);
84        } else {
85          // Consent was denied/revoked.
86          self.consentDenied(context, config);
87        }
88      });
89    },
90  };
91})(Drupal);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.