YouTube - add video ID text field

Usable for copying a video ID to a clipboard for downloading via youtube-dl. By default it also prefixes the ID with "--" if it starts with a dash (this can be disabled).

目前为 2015-04-17 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name YouTube - add video ID text field
  3. // @namespace monnef.tk
  4. // @description Usable for copying a video ID to a clipboard for downloading via youtube-dl. By default it also prefixes the ID with "--" if it starts with a dash (this can be disabled).
  5. // @include http://www.youtube.com/watch*
  6. // @include https://www.youtube.com/watch*
  7. // @version 2
  8. // @grant none
  9. // @require http://code.jquery.com/jquery-2.1.3.min.js
  10. // ==/UserScript==
  11.  
  12. // if you don't want prefixing, replace the word "true" with "false" on the next line
  13. var prefixEnabled = true;
  14. var textId = "textId";
  15.  
  16. function textFieldClicked() {
  17. var e = $(this);
  18. e.select();
  19. }
  20.  
  21. function updateLinkValue(value) {
  22. $("#" + textId).attr("value", value);
  23. }
  24.  
  25. function getLinkMatchFromLocation() {
  26. return window.location.href.match(/^.*watch(\?v=([^&]+)|\?(.*)).*$/);
  27. }
  28.  
  29. function getLinkFromLocation() {
  30. var urlMatch = getLinkMatchFromLocation();
  31. if (urlMatch.length >= 2) {
  32. var link = urlMatch[2];
  33. if (prefixEnabled && link[0] === '-') link = "-- " + link;
  34. return link;
  35. }
  36. return "";
  37. }
  38.  
  39. function getVideoElement() {
  40. return $("video.video-stream");
  41. }
  42.  
  43. function insertInput() {
  44. $("#movie_player").each(function() {
  45. var e = $(this);
  46. var link = getLinkFromLocation();
  47. if (link) {
  48. e.parent().after($("<input />").attr("type", "text").css("margin-top", "10px").css("border", "0").css("box-shadow", "0px 1px 2px rgba(0, 0, 0, 0.1)").css("font-size", "200%").attr("id", textId).click(textFieldClicked));
  49. updateLinkValue(link);
  50. }
  51. });
  52. }
  53.  
  54. function refreshTextLink(){
  55. var link = getLinkFromLocation();
  56. console.log("refreshing text link: " + link);
  57. if (link) updateLinkValue(link);
  58. }
  59.  
  60. // beacuse mutation observers are broken in GreaseMonkey :(
  61. function installDomEventListener() {
  62. getVideoElement().each(function() {
  63. this.parentNode.addEventListener("DOMNodeInserted", function(e) {
  64. console.log("modified (insertion): src=" + getVideoElement().attr("src"));
  65. refreshTextLink();
  66. }, false);
  67. });
  68. }
  69.  
  70. insertInput();
  71. installDomEventListener();
  72. refreshTextLink();