5ch_youtube_embed

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

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