Append Hostname to Window Title

Append the domain name of the site to the window title. Helps offer a hint to tools like AHK so that they can identify the site loaded.

// ==UserScript==
// @name         Append Hostname to Window Title
// @namespace    https://gf.qytechs.cn/users/77886
// @version      0.4
// @description  Append the domain name of the site to the window title. Helps offer a hint to tools like AHK so that they can identify the site loaded.
// @author       muchtall
// @include      *://*/*
// @match        *://*/*
// @grant        none
// ==/UserScript==

(
  function() {
    'use strict';

    /// Set the window title on page load
    window.addEventListener('load', function () {
      document.title = document.title + " - (" + window.location.hostname + ")";
    });

    // Workaround to sites that insist on changing the title of the window after the page loads
    setTimeout(function() {
        var target = document.querySelector('title');
        var observer = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                var re = new RegExp(" - \\(" + window.location.hostname + "\\)$", "");
                if ( ! re.test(document.title) ) {
                  console.log('Title before "%s"', document.title);
                  document.title = document.title + " - (" + window.location.hostname + ")";
                  console.log('Title after "%s"', document.title);
                }
            });
        });
        var config = {
            childList: true,
        };
        observer.observe(target, config);
    }, 100);
  }
)();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址