Block visibility tracking on Class

Blocks page visibility / unload tracking on the target page only

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Block visibility tracking on Class
// @namespace    https://upc.blackboard.com
// @author       enzocipher
// @license      MIT
// @version      1.2
// @description  Blocks page visibility / unload tracking on the target page only
// @match        *://*.upc.blackboard.com/*
// @match        *://*.upc.class.com/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(() => {
  'use strict';

  const targetHost = location.hostname.endsWith('class.com');

  if (!targetHost) return;

  const forceVisible = () => 'visible';
  const forceFalse = () => false;

  try {
    Object.defineProperty(Document.prototype, 'visibilityState', {
      get: forceVisible,
      configurable: true,
    });
  } catch {}

  try {
    Object.defineProperty(Document.prototype, 'hidden', {
      get: forceFalse,
      configurable: true,
    });
  } catch {}

  try {
    Object.defineProperty(document, 'visibilityState', {
      get: forceVisible,
      configurable: true,
    });
  } catch {}

  try {
    Object.defineProperty(document, 'hidden', {
      get: forceFalse,
      configurable: true,
    });
  } catch {}

  try {
    Object.defineProperty(window, 'visibilityState', {
      get: forceVisible,
      configurable: true,
    });
  } catch {}

  try {
    Object.defineProperty(window, 'hidden', {
      get: forceFalse,
      configurable: true,
    });
  } catch {}

  try {
    Object.defineProperty(Document.prototype, 'hasFocus', {
      value: () => true,
      configurable: true,
    });
  } catch {}

  const originalAddEventListener = EventTarget.prototype.addEventListener;
  const blockedEvents = new Set(['visibilitychange', 'pagehide', 'beforeunload', 'blur']);

  EventTarget.prototype.addEventListener = function (type, listener, options) {
    const isTargetDocument = this === document;
    const isTargetWindow = this === window;

    if ((isTargetDocument && blockedEvents.has(type)) || (isTargetWindow && blockedEvents.has(type))) {
      return;
    }

    return originalAddEventListener.call(this, type, listener, options);
  };
})();