No Embed Youtube

replace embed iframe, object with anchor link.

当前为 2014-05-23 提交的版本,查看 最新版本

  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.0
  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. }else{
  28. let xpath = "//iframe[contains(@src,'youtube.com/embed/')]|" +
  29. "//object[./param[contains(@value,'youtube.com/v/')]]|" +
  30. "//embed[contains(@src,'youtube.com/v/') and not(ancestor::object)]";
  31. let unEmbed = function(node){
  32. let result = document.evaluate(
  33. xpath, node, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  34. let i = 0;
  35. let element = null;
  36. while(element = result.snapshotItem(i++)){
  37. // iframe or embed
  38. let url = element.src;
  39. // object
  40. if(!url){
  41. for(let i = 0; i < element.childNodes.length; i++){
  42. let pa = element.childNodes[i];
  43. if(pa.getAttribute("name") == "movie"){
  44. url = pa.getAttribute("value");
  45. break;
  46. }
  47. }
  48. }
  49. if(!url){
  50. console.log("can't find url!", element);
  51. continue;
  52. }
  53. let id = url.match(/(embed|v)\/(.+?)(\?|&|$)/)[2];
  54. let a = document.createElement("a");
  55. let pageUrl = "http://www.youtube.com/watch?v=" + id;
  56. a.appendChild(document.createTextNode(pageUrl));
  57. a.setAttribute("href", pageUrl.replace("http:", ""));
  58. a.setAttribute("target", "_blank");
  59. a.className = "unembed";
  60. element.parentNode.replaceChild(a, element);
  61. }
  62. };
  63. document.addEventListener("DOMNodeInserted", function(e){
  64. unEmbed(e.target);
  65. // alert("pause");
  66. }, false);
  67. }