No Embed Youtube

replace embed iframe, object with anchor link.

目前为 2014-05-27 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name No Embed Youtube
  3. // @description replace embed iframe, object with anchor link.
  4. // @namespace eight04.blogspot.com
  5. // @include http*
  6. // @exclude http://www.youtube.com/watch*
  7. // @exclude https://www.youtube.com/watch*
  8. // @version 1.1
  9. // @grant none
  10. // @run-at document-start
  11. // ==/UserScript==
  12.  
  13. /*
  14. embed code from youtube:
  15. <iframe width="420" height="315" src="//www.youtube.com/embed/MOyueLEw2xo" frameborder="0" allowfullscreen></iframe>
  16. <object width="420" height="315"><param name="movie" value="//www.youtube.com/v/MOyueLEw2xo?hl=zh_TW&amp;version=3"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="//www.youtube.com/v/MOyueLEw2xo?hl=zh_TW&amp;version=3" type="application/x-shockwave-flash" width="420" height="315" allowscriptaccess="always" allowfullscreen="true"></embed></object>
  17. */
  18.  
  19. "use strict";
  20. /*
  21. if(/youtube\.com\/(v|embed)\//.test(window.location.href)){
  22. // stop iframe loading
  23. // window.location.href = "about:blank";
  24. // is there a better way to pause page?
  25. // window.stop();
  26. // alert("pause");
  27. console.log(unsafeWindow.parent.unEmbed);
  28. }else{
  29. */
  30.  
  31. let xpath = "//iframe[contains(@src,'youtube.com/embed/')]|" +
  32. "//object[./param[contains(@value,'youtube.com/v/')]]|" +
  33. "//embed[contains(@src,'youtube.com/v/') and not(ancestor::object)]";
  34.  
  35. let unEmbed = function(node){
  36. let result = document.evaluate(
  37. xpath, node, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  38. let i = 0;
  39. let element = null;
  40. while(element = result.snapshotItem(i++)){
  41. // iframe or embed
  42. let url = element.src;
  43. // object
  44. if(!url){
  45. for(let i = 0; i < element.childNodes.length; i++){
  46. let pa = element.childNodes[i];
  47. if(pa.getAttribute("name") == "movie"){
  48. url = pa.getAttribute("value");
  49. break;
  50. }
  51. }
  52. }
  53. if(!url){
  54. console.log("can't find url!", element);
  55. continue;
  56. }
  57. let id = url.match(/(embed|v)\/(.+?)(\?|&|$)/)[2];
  58. let a = document.createElement("a");
  59. let pageUrl = "http://www.youtube.com/watch?v=" + id;
  60. a.appendChild(document.createTextNode(pageUrl));
  61. a.setAttribute("href", pageUrl.replace("http:", ""));
  62. a.setAttribute("target", "_blank");
  63. a.className = "unembed";
  64. element.parentNode.replaceChild(a, element);
  65. }
  66. };
  67. document.addEventListener("DOMNodeInserted", function(e){
  68. unEmbed(e.target);
  69. }, false);
  70.  
  71. // }