topic_solve

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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];
  }
})();