GOG: Static and Better Page Titles

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

当前为 2025-03-12 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GOG: Static and Better Page Titles
  3. // @namespace Violentmonkey Scripts
  4. // @match https://www.gog.com/*
  5. // @grant none
  6. // @version 1.0
  7. // @author GreasyBastard
  8. // @license AGPLv3
  9. // @description Disable annoying dynamic updates to page titles and improve them overall.
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Function to set the title based on og:title meta tag
  16. const setTitleFromMetaTag = () => {
  17. const metaTag = document.querySelector('meta[property="og:title"]');
  18. if (metaTag && metaTag.content) {
  19. document.title = metaTag.content;
  20. }
  21. };
  22.  
  23. // Function to set the title based on the query parameter
  24. const setTitleFromQueryParam = () => {
  25. const urlParams = new URLSearchParams(window.location.search);
  26. const queryTerm = urlParams.get('query');
  27. if (queryTerm) {
  28. document.title = `> ${decodeURIComponent(queryTerm)}`;
  29. }
  30. };
  31.  
  32. // Function to set the title from the first h1 tag for non-game paths
  33. const setTitleFromFirstH1 = () => {
  34. const pathPart = window.location.pathname.split('/')[3]; // Extract the 3rd part of the URL path
  35. if (pathPart && pathPart !== 'game' && pathPart !== 'games') {
  36. const firstH1 = document.querySelector('h1');
  37. if (firstH1) {
  38. document.title = firstH1.textContent.trim();
  39. }
  40. }
  41. };
  42.  
  43. // Function to set the title to "GOG.com" for homepage or /en/ root
  44. const setTitleForHomepage = () => {
  45. const pathParts = window.location.pathname.split('/').filter(part => part.trim() !== '');
  46. console.log('Path Parts:', pathParts); // Debugging output to check the pathname parts
  47. if (pathParts.length <= 1) { // This should cover "/" and "/en/"
  48. document.title = "GOG.com";
  49. }
  50. };
  51.  
  52. // Function to set the title to the error code for error pages (like /404, /403, etc.)
  53. const setTitleForErrorPage = () => {
  54. const pathParts = window.location.pathname.split('/').filter(part => part.trim() !== '');
  55. const errorCode = pathParts[pathParts.length - 1]; // The last part of the URL path
  56.  
  57. // Check if the last part of the path is a number (error code like 404, 403, etc.)
  58. if (!isNaN(errorCode)) {
  59. document.title = `Error ${errorCode}`;
  60. }
  61. };
  62.  
  63. // Function to lock the document title
  64. const lockTitle = () => {
  65. const originalTitle = document.title;
  66. Object.defineProperty(document, 'title', {
  67. configurable: false,
  68. enumerable: true,
  69. get: () => originalTitle,
  70. set: () => {}
  71. });
  72. };
  73.  
  74. // Initialize
  75. setTitleForHomepage();
  76. setTitleFromMetaTag();
  77. setTitleFromQueryParam();
  78. setTitleFromFirstH1();
  79. setTitleForErrorPage();
  80. lockTitle();
  81. })();
  82.