5ch_youtube_embed

5ちゃんねるのつべを埋め込み表示する。

当前为 2020-02-04 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name 5ch_youtube_embed
  3. // @namespace https://catherine.v0cyc1pp.com/5ch_youtube_embed.user.js
  4. // @include http://*.5ch.net/*
  5. // @include https://*.5ch.net/*
  6. // @include http://*.bbspink.com/*
  7. // @include https://*.bbspink.com/*
  8. // @author greg10
  9. // @run-at document-end
  10. // @license GPL 3.0
  11. // @version 0.3
  12. // @grant none
  13. // @description 5ちゃんねるのつべを埋め込み表示する。
  14. // ==/UserScript==
  15. console.log("5ch_youtube_embed start");
  16.  
  17.  
  18.  
  19. /*
  20. https://youtu.be/9_y6nFjoVp4
  21. https://www.youtube.com/watch?v=9_y6nFjoVp4&feature=youtu.be
  22. */
  23. function get_embed_url(id) {
  24. //https://www.youtube.com/embed/VIDEO_ID
  25. var embed_url = "https://www.youtube.com/embed/" + id;
  26. console.log("embed_url=" + embed_url);
  27. return embed_url;
  28. }
  29.  
  30. function main() {
  31. document.querySelectorAll("a").forEach(function(elem) {
  32. var thiselem = elem;
  33.  
  34. var str = elem.innerText;
  35.  
  36. if (elem.getAttribute("flag5ch_youtube_embed") == "done") {
  37. return;
  38. }
  39. elem.setAttribute("flag5ch_youtube_embed", "done");
  40.  
  41.  
  42. //console.log("str="+str);
  43. var result = str.match(/https?:\/\/youtu.be\/(.+)$/i);
  44. var result2 = str.match(/https?:\/\/(www|m).youtube.com\/watch\?v=([^&]+)$/i);
  45. if (result == null && result2 == null) {
  46. return;
  47. }
  48. var id = "";
  49. if ( result != null ) {
  50. id = result[1];
  51. } else if ( result2 != null ) {
  52. id = result2[2];
  53. }
  54. console.log("youtube matched, str=" + str);
  55.  
  56. var embed_url = get_embed_url(id);
  57.  
  58.  
  59. var elem_p = document.createElement("p");
  60.  
  61. // iframe
  62. var iframe = document.createElement("iframe");
  63. iframe.src = embed_url;
  64. iframe.id = "ytplayer";
  65. iframe.type = "text/html";
  66. iframe.width = 320;
  67. iframe.height = 180;
  68. iframe.frameborder = 0;
  69.  
  70. thiselem.parentNode.insertBefore(elem_p, thiselem.nextElementSibling);
  71. elem_p.parentNode.insertBefore(iframe, elem_p.nextElementSibling);
  72.  
  73. });
  74. }
  75.  
  76. main();
  77.  
  78. var observer = new MutationObserver(function(mutations) {
  79. observer.disconnect();
  80. main();
  81. observer.observe(document, config);
  82. });
  83.  
  84. var config = {
  85. attributes: false,
  86. childList: true,
  87. characterData: false,
  88. subtree: true
  89. };
  90.  
  91. observer.observe(document, config);