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.

  1. // ==UserScript==
  2. // @name Append Hostname to Window Title
  3. // @namespace https://greasyfork.org/users/77886
  4. // @version 0.4
  5. // @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.
  6. // @author muchtall
  7. // @include *://*/*
  8. // @match *://*/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (
  13. function() {
  14. 'use strict';
  15.  
  16. /// Set the window title on page load
  17. window.addEventListener('load', function () {
  18. document.title = document.title + " - (" + window.location.hostname + ")";
  19. });
  20.  
  21. // Workaround to sites that insist on changing the title of the window after the page loads
  22. setTimeout(function() {
  23. var target = document.querySelector('title');
  24. var observer = new MutationObserver(function(mutations) {
  25. mutations.forEach(function(mutation) {
  26. var re = new RegExp(" - \\(" + window.location.hostname + "\\)$", "");
  27. if ( ! re.test(document.title) ) {
  28. console.log('Title before "%s"', document.title);
  29. document.title = document.title + " - (" + window.location.hostname + ")";
  30. console.log('Title after "%s"', document.title);
  31. }
  32. });
  33. });
  34. var config = {
  35. childList: true,
  36. };
  37. observer.observe(target, config);
  38. }, 100);
  39. }
  40. )();