Title Manager

Manage page titles per (sub)domain. Shift-T-M for options.

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

  1. // ==UserScript==
  2. // @name Title Manager
  3. // @namespace grom
  4. // @description Manage page titles per (sub)domain. Shift-T-M for options.
  5. // @include http*
  6. // @grant GM_getValue
  7. // @grant GM_setValue
  8. // @grant GM_deleteValue
  9. // @version 1.0.20140312
  10. // @run-at document-end
  11. // ==/UserScript==
  12.  
  13.  
  14. if ( GM_getValue === 'undefined' || GM_setValue === 'undefined' ) {
  15. alert('=== Title Manager ===\n\nUnable to load or store values.\
  16. \nPlease use supported script managers:\nGreasemonkey, Tampermonkey...');
  17. }
  18.  
  19. var domains = GM_getValue('domains', '').replace(/\./g, '\\.');
  20. var match = window.location.host.toLowerCase().match(domains);
  21. if (match) {
  22. var _find = GM_getValue(match[0] + '-find', '');
  23. if (_find.match(/^regex:/)) {
  24. var _find = _find.replace(/^regex:/, '');
  25. var _find = new RegExp(_find, 'i');
  26. }
  27. var _replaceWith = GM_getValue(match[0] + '-with', '');
  28. }
  29.  
  30. var t = document.querySelector('title');
  31. var tOriginal = t.innerHTML;
  32.  
  33. if (t && _find) {
  34. t.innerHTML = t.innerHTML.replace(_find,_replaceWith);
  35. }
  36.  
  37. // separators
  38. var sep1 = '========';
  39. var sep2 = '--------';
  40.  
  41. function tm_add() {
  42. var d = document,
  43. _domain = d.getElementById('tm-domain').value.toLowerCase(),
  44. _domainCheck = /^[a-z0-9_\.-]+$/;
  45. if (!_domain.match(_domainCheck)) {
  46. d.getElementById('tm-error-notifier').innerHTML = 'Domain invalid. Please use letters a-z, numbers 0-9, underscore _, minus - or dot .';
  47. return false;
  48. }
  49. var _find = d.getElementById('tm-find').value;
  50. var _with = d.getElementById('tm-with').value;
  51. // store values
  52. if (_domain && _find) {
  53. var domains = GM_getValue('domains', '');
  54. if (!domains) {
  55. GM_setValue('domains', _domain);
  56. }
  57. else {
  58. var match = _domain.replace(/\./g, '\\.');
  59. var match = new RegExp('(^|\\|)' + match + '($|\\|)');
  60. if (!domains.match(match)) {
  61. var domains = domains + '|' + _domain;
  62. GM_setValue('domains', domains);
  63. }
  64. }
  65. GM_setValue(_domain + '-find', _find);
  66. if (_with) {
  67. var _withRegex = d.getElementById('tm-with');
  68. // if not adding as regex
  69. if (!_withRegex.getAttribute('regex')) {
  70. // crazy dollar sign escaping;
  71. var _with = _with.replace(/\$/g, '$$$$');
  72. }
  73. GM_setValue(_domain + '-with', _with);
  74. _withRegex.removeAttribute('regex');
  75. }
  76. }
  77. }
  78.  
  79. function tm_addRegex() {
  80. var _find = document.getElementById('tm-find').value;
  81. if (!_find.match(/^regex:/)) {
  82. _find = _find.replace(/^/, 'regex:');
  83. document.getElementById('tm-find').value = _find;
  84. }
  85. document.getElementById('tm-with').setAttribute('regex', 'true');
  86. tm_add();
  87. }
  88.  
  89. function tm_manage() {
  90. var d = document;
  91. // d.documentElement.innerHTML is alternative to d.write("");
  92. d.documentElement.innerHTML = '';
  93. var domains = GM_getValue('domains', '');
  94. if (domains) {
  95. var domains = domains.split('|');
  96. for(var i = 0, j = domains.length; i < j; i++) {
  97. var _find = GM_getValue(domains[i] + '-find', '');
  98. var _replaceWith = GM_getValue(domains[i] + '-with', '');
  99. var box = d.createElement('div');
  100. box.className = 'item';
  101. box.innerHTML = '<span class="doms">' + domains[i] + '</span><br /><span class="find">' + _find + '</span>\
  102. <br /><span class="with">' + _replaceWith + '</span><br /><button>Remove</button><br /><span>' + sep1 + '</span>';
  103. if (d.body) d.body.appendChild(box);
  104. else d.documentElement.appendChild(box);
  105. box.getElementsByTagName('button')[0].addEventListener('click', tm_removeItem);
  106. }
  107. }
  108. var impList = d.createElement('textarea');
  109. impList.id = 'tm-import-list';
  110. d.documentElement.appendChild(impList);
  111. var imp = d.createElement('input');
  112. imp.type = 'submit'; imp.id = 'tm-import'; imp.value = 'Import';
  113. d.documentElement.appendChild(imp);
  114. imp.addEventListener('click', tm_import);
  115. }
  116.  
  117. function tm_removeItem() {
  118. var item = this.parentNode;
  119. var _domain = item.getElementsByClassName('doms')[0].innerHTML;
  120. GM_deleteValue(_domain + '-find');
  121. GM_deleteValue(_domain + '-with');
  122. var domains = GM_getValue('domains', '');
  123. var match = _domain.replace(/\./g,"\\.");
  124. var match = new RegExp('(^|\\|)' + match + '($|\\|)'); // match: (^ or |) + single/current domain + ($ or |)
  125. var domains = domains.replace(match, '$1').replace(/(\|)\||\|$/g, '$1'); // replace: matched single domain with ^ or |; replace: || with | or remove |$
  126. GM_setValue('domains', domains);
  127. item.parentNode.removeChild(item);
  128. }
  129.  
  130. function tm_import() {
  131. var d = document,
  132. list = d.getElementById('tm-import-list').value;
  133. var list = list.match(/.+/g).join(sep2).replace(/(--------)+========$/, '').split(sep1+sep2);
  134. if (!d.getElementById('tm-add')) tm_QuickMenu();
  135. for(var i = 0, j = list.length; i < j; i++) {
  136. var listB = list[i].split(sep2);
  137. d.getElementById('tm-domain').value = listB[0];
  138. d.getElementById('tm-find').value = listB[1];
  139. if (listB[2]) {
  140. d.getElementById('tm-with').value = listB[2];
  141. d.getElementById('tm-with').setAttribute('regex', 'true');
  142. }
  143. if (listB[0] && listB[1]) tm_add();
  144. }
  145. tm_manage();
  146. }
  147.  
  148. function tm_QuickMenu() {
  149. var d = document,
  150. box = d.createElement('div');
  151. box.style = 'text-align:center!important;';
  152. box.innerHTML = '<br /><hr><h1>Title Manager</h1><p>Full title: "<strong>' + tOriginal + '</strong>"</p>\
  153. <p id="tm-error-notifier">Regex supported only in "Search for"; use single backslash <strong>\\</strong> for escaping characters.</p>\
  154. <p>Domain: <input type="text" id="tm-domain" value="' + window.location.host.toLowerCase() + '" /></p>\
  155. <p>Search for: <input type="text" id="tm-find" value="' + tOriginal + '" /></p>\
  156. <p>Replace with: <input type="text" id="tm-with" value="" /></p>\
  157. <p><input type="submit" id="tm-add" value="Add" /> or <input type="submit" id="tm-add-regex" value="Add as regex" /></p>\
  158. <p><input type="submit" id="tm-manage" value="View and manage all title rules" /></p><br /><br />';
  159. if (d.body) d.body.appendChild(box);
  160. else d.documentElement.appendChild(box);
  161. box.scrollIntoView();
  162. d.getElementById('tm-add').addEventListener('click', tm_add);
  163. d.getElementById('tm-add-regex').addEventListener('click', tm_addRegex);
  164. d.getElementById('tm-manage').addEventListener('click', tm_manage);
  165. }
  166.  
  167. // trigger for QuickMenu: Shift+T+M
  168. var kbd = [];
  169. onkeydown = onkeyup = function(e) {
  170. kbd[e.keyCode] = e.type == 'keydown';
  171. if (kbd[16] && kbd[84] && kbd[77]) {
  172. tm_QuickMenu();
  173. kbd = [];
  174. return false;
  175. }
  176. }

QingJ © 2025

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