URLs Need Titles

When you paste a URL to a friend, it is useful if it contains the title of the page. This script adds these missing titles for common websites using # part of URL. In other words, it turns non-semantic URLs into semantic URLs!

  1. // ==UserScript==
  2. // @name URLs Need Titles
  3. // @namespace UNT
  4. // @description When you paste a URL to a friend, it is useful if it contains the title of the page. This script adds these missing titles for common websites using # part of URL. In other words, it turns non-semantic URLs into semantic URLs!
  5. // @version 1.3.3
  6. // @include http://*/*
  7. // @include https://*/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. // For example, this script will change this address:
  12. //
  13. // http://www.imdb.com/title/tt1155592/
  14. //
  15. // into this address:
  16. //
  17. // http://www.imdb.com/title/tt1155592/#Man_on_Wire_(2008)
  18. //
  19. // which is far more useful to paste to other people.
  20.  
  21. var overwriteExistingHash = true;
  22.  
  23. var rules = [
  24.  
  25. /* An example rule:
  26.  
  27. {
  28. changeTitle: true / false, Some of the rules do not change the URL,
  29. but change the page title instead. This
  30. can be useful for bookmarking.
  31.  
  32. hostMatch: "youtube.TLD", All subdomains will be accepted, e.g.
  33. somewhere.youtube.com. TLD will match one
  34. or two toplevel domain elements, e.g.
  35. "youtube.ath.cx" or "youtube.net".
  36.  
  37. pathMatch: "/watch", This regex will be wrapped with ^..$ so use
  38. .* at either end for wildcards. Note that
  39. path does not include the search part of
  40. the URL, "?q=squash&type=image". Filtering
  41. on this part of the URL may be added later.
  42. If left blank all paths will be accepted.
  43.  
  44. getTitle: function(){...} A function which returns a sensible title
  45. for this page. If left blank will just
  46. grab document.title.
  47. }
  48.  
  49. */
  50.  
  51. {
  52. hostMatch: "youtube.TLD",
  53. pathMatch: "/watch",
  54. getTitle: function(){
  55. return document.title.replace(/ - YouTube$/,'').replace(/^\([\d]*\) /, '');
  56. }
  57. },
  58.  
  59. {
  60. hostMatch: "xkcd.TLD",
  61. pathMatch: ".*[0-9]+/",
  62. getTitle: function(){ return document.title.replace(/^xkcd: /,''); }
  63. },
  64.  
  65. {
  66. hostMatch: "imdb.TLD",
  67. pathMatch: ".*title.*",
  68. getTitle: function(){ return document.title.replace(/ - IMDb/,''); }
  69. },
  70.  
  71. {
  72. hostMatch: "pouet.net",
  73. pathMatch: ".*",
  74. getTitle: function(){ return document.title.replace(/ :: pouët.net$/,''); }
  75. },
  76.  
  77. {
  78. hostMatch: "userscripts.org",
  79. pathMatch: "/scripts/show/.*",
  80. getTitle: function(){ return document.title.replace(" for Greasemonkey",''); }
  81. },
  82.  
  83. {
  84. hostMatch: "bbc.co.uk",
  85. pathMatch: "/news/.*",
  86. getTitle: function(){ return document.title.replace(/BBC News - /,''); }
  87. },
  88.  
  89. {
  90. hostMatch: "imgur.com",
  91. pathMatch: "/.+",
  92. getTitle: function(){ return document.title.replace(/ - Imgur/,''); }
  93. },
  94.  
  95. {
  96. hostMatch: "9gag.com",
  97. pathMatch: "/gag/.*",
  98. getTitle: function(){ return document.title.replace(/ - 9GAG$/,''); }
  99. },
  100.  
  101. {
  102. changeUrl: true,
  103. changeTitle: true,
  104. hostMatch: "codepen.io",
  105. pathMatch: "/..*", /* Not the front page */
  106. getTitle: function(){
  107. // They stopped putting " - CodePen" at the end of their titles. We shall put it back, to aid bookmarking.
  108. if (!document.title.match(/CodePen$/)) {
  109. document.title = document.title + ' - CodePen';
  110. }
  111. return document.title.replace(/ - CodePen$/,'');
  112. }
  113. },
  114.  
  115. {
  116. hostMatch: "unrealadmin.org",
  117. getTitle: function(){ return document.title.replace(/ - The Unreal Admins Page .*/,''); }
  118. },
  119.  
  120. {
  121. hostMatch: "vim.org",
  122. getTitle: function(){
  123. // When viewing user profile page
  124. if (document.location.pathname.indexOf("/profile.php") >= 0) {
  125. var username = document.evaluate("//td[string(.)='user name']/following-sibling::*",document,null,6,null).snapshotItem(0).textContent; // maybe I should start using jquery
  126. return username+"'s profile";
  127. }
  128. // Otherwise (good when viewing a script page)
  129. return document.title.replace(/ - .*/,'');
  130. }
  131. },
  132.  
  133. // This was not updating the URL, but the window title, to include the repo description.
  134. // Mid 2016: They are actually doing this now, so this script is no longer needed.
  135. //{
  136. // changeTitle: true,
  137. // hostMatch: "github.com",
  138. // pathMatch: "/[^/]*/[^/]*/*",
  139. // getTitle: function(){
  140. // //var repoDescriptionElems = document.getElementsByClassName("repository-description");
  141. // // Late 2015
  142. // var repoDescriptionElems = document.getElementsByClassName("repository-meta-content");
  143. // if (repoDescriptionElems) {
  144. // var repoDescription = repoDescriptionElems[0].textContent.trim();
  145. // // For a while their titles were "<author_name>/<repo_name> <weird-dot> GitHub" but right now they just have the repo path.
  146. // document.title = document.title.replace(/(\s+[^ ]*\s+GitHub|)$/, '') + " - "+repoDescription;
  147. // }
  148. // return null;
  149. // }
  150. //},
  151.  
  152. {
  153. changeTitle: true,
  154. hostMatch: "engineers.sg",
  155. pathMatch: "/video/.*",
  156. getTitle: function(){
  157. var titleElement = document.querySelector('h4');
  158. if (titleElement && titleElement.textContent) {
  159. document.title = titleElement.textContent + " - Engineers.SG";
  160. }
  161. return null;
  162. }
  163. }
  164.  
  165. ];
  166.  
  167. function check() {
  168. if (document.location.hash && !overwriteExistingHash) {
  169. return;
  170. }
  171. rules.forEach(checkRule);
  172. }
  173.  
  174. function checkRule(rule) {
  175. var hostRegexp = '(^|\\.)' + rule.hostMatch.replace(/\.TLD$/, "(\\.[^.]*$|\\.[^.]*\\.[^.]*$)") + '$';
  176. if (document.location.host.match(hostRegexp)) {
  177. if (rule.pathMatch) {
  178. var pathRegexp = '^' + rule.pathMatch + '$';
  179. if (!document.location.pathname.match(pathRegexp)) {
  180. return false;
  181. }
  182. }
  183. var newTitle = ( rule.getTitle ? rule.getTitle() : document.title );
  184. if (newTitle == '' || newTitle == null) {
  185. console.log("Failed to get new title for "+document.location+" from "+document.title);
  186. }
  187. setTitle(newTitle);
  188. }
  189. }
  190.  
  191. function setTitle(title) {
  192. if (title) {
  193. // "_"s paste better into IRC, since " "s become "%20"s which are hard to read. The second and third parts trim "_"s and newlines from the start and end of the string.
  194. title = title.replace(/ /g,'_').replace(/^[\r\n_]*/,'').replace(/[\r\n_]*$/,'');
  195. var strippedHref = document.location.href.replace(/#.*/,'');
  196. if (history.replaceState) {
  197. // Does not cause YouTube to reload the page
  198. history.replaceState(undefined, document.title, strippedHref + '#' + title);
  199. } else {
  200. // Does not alter browser history
  201. document.location.replace(strippedHref + '#' + title);
  202. // Was sometimes a better alternative when Chrome was crashing
  203. //document.location.hash = title;
  204. }
  205. }
  206. }
  207.  
  208.  
  209. // 2010/11: Waiting a bit can prevent crashing (e.g. YouTube in Chrome).
  210. setTimeout(check,5000);
  211.  
  212. // vim: ts=4 sw=4 expandtab

QingJ © 2025

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