GOG: Static and Better Page Titles

Disable annoying dynamic updates to page titles and improve them overall.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        GOG: Static and Better Page Titles
// @namespace   Violentmonkey Scripts
// @match       https://www.gog.com/*
// @grant       none
// @version     1.0
// @author      GreasyBastard
// @license     AGPLv3
// @description Disable annoying dynamic updates to page titles and improve them overall.
// ==/UserScript==

(function() {
  'use strict';

  // Function to set the title based on og:title meta tag
  const setTitleFromMetaTag = () => {
    const metaTag = document.querySelector('meta[property="og:title"]');
    if (metaTag && metaTag.content) {
      document.title = metaTag.content;
    }
  };

  // Function to set the title based on the query parameter
  const setTitleFromQueryParam = () => {
    const urlParams = new URLSearchParams(window.location.search);
    const queryTerm = urlParams.get('query');
    if (queryTerm) {
      document.title = `> ${decodeURIComponent(queryTerm)}`;
    }
  };

  // Function to set the title from the first h1 tag for non-game paths
  const setTitleFromFirstH1 = () => {
    const pathPart = window.location.pathname.split('/')[3]; // Extract the 3rd part of the URL path
    if (pathPart && pathPart !== 'game' && pathPart !== 'games') {
      const firstH1 = document.querySelector('h1');
      if (firstH1) {
        document.title = firstH1.textContent.trim();
      }
    }
  };

  // Function to set the title to "GOG.com" for homepage or /en/ root
  const setTitleForHomepage = () => {
    const pathParts = window.location.pathname.split('/').filter(part => part.trim() !== '');
    console.log('Path Parts:', pathParts);  // Debugging output to check the pathname parts
    if (pathParts.length <= 1) { // This should cover "/" and "/en/"
      document.title = "GOG.com";
    }
  };

  // Function to set the title to the error code for error pages (like /404, /403, etc.)
  const setTitleForErrorPage = () => {
    const pathParts = window.location.pathname.split('/').filter(part => part.trim() !== '');
    const errorCode = pathParts[pathParts.length - 1];  // The last part of the URL path

    // Check if the last part of the path is a number (error code like 404, 403, etc.)
    if (!isNaN(errorCode)) {
      document.title = `Error ${errorCode}`;
    }
  };

  // Function to lock the document title
  const lockTitle = () => {
    const originalTitle = document.title;
    Object.defineProperty(document, 'title', {
      configurable: false,
      enumerable: true,
      get: () => originalTitle,
      set: () => {}
    });
  };

  // Initialize
  setTitleForHomepage();
  setTitleFromMetaTag();
  setTitleFromQueryParam();
  setTitleFromFirstH1();
  setTitleForErrorPage();
  lockTitle();
})();