topic_solve

Send the same topic JSON track_visit request when a linux.do topic opens in a background tab.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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 यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

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

Advertisement:

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

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

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

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

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

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

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

Advertisement:

// ==UserScript==
// @name         topic_solve
// @namespace    local-linuxdo
// @version      0.4.0
// @description  Send the same topic JSON track_visit request when a linux.do topic opens in a background tab.
// @match        https://linux.do/t/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(() => {
  "use strict";

  if (document.visibilityState !== "hidden") {
    return;
  }

  const topicId = topicIdFromPath(location.pathname);
  const storageKey = `topic_solve:${topicId}`;

  if (sessionStorage.getItem(storageKey)) {
    return;
  }

  sessionStorage.setItem(storageKey, String(Date.now()));

  fetch(`/t/${topicId}/1.json?track_visit=true&forceLoad=true`, {
    method: "GET",
    credentials: "include",
    cache: "no-store",
    headers: {
      Accept: "application/json, text/javascript, */*; q=0.01",
      "X-Requested-With": "XMLHttpRequest",
      "Discourse-Present": "true",
      "Discourse-Track-View": "true",
      "Discourse-Track-View-Topic-Id": topicId,
    },
  })
    .then((response) => {
      console.debug("[linuxdo background topic track_visit]", {
        topicId,
        status: response.status,
        trackView: response.headers.get("x-discourse-trackview"),
        browserPageView: response.headers.get("x-discourse-browserpageview"),
      });

      if (!response.ok) {
        sessionStorage.removeItem(storageKey);
      }
    })
    .catch((error) => {
      sessionStorage.removeItem(storageKey);
      console.error("[linuxdo background topic track_visit]", error);
    });

  function topicIdFromPath(pathname) {
    const match = pathname.match(/^\/t\/(?:[^/]+\/)?(\d+)(?:\/|$)/);

    if (!match) {
      throw new Error(`Unsupported linux.do topic URL: ${pathname}`);
    }

    return match[1];
  }
})();