No Embed Youtube

replace embed iframe, object with anchor link.

目前為 2015-09-28 提交的版本,檢視 最新版本

  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/*
  7. // @exclude https://www.youtube.com/*
  8. // @version 2.0.0
  9. // @run-at document-start
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. "use strict";
  14.  
  15. var xpath = "//iframe[contains(@src,'youtube.com/embed/') and not(ancestor::*[@id='YTLT-player'])]|" +
  16. "//iframe[contains(@src,'youtube.com/v/') and not(ancestor::*[@id='YTLT-player'])]|" +
  17. "//object[./param[contains(@value,'youtube.com/v/')] and not(ancestor::*[@id='YTLT-player'])]|" +
  18. "//embed[contains(@src,'youtube.com/v/') and not(ancestor::object) and not(ancestor::*[@id='YTLT-player'])]";
  19.  
  20. var unEmbed = function(node){
  21.  
  22. var result = document.evaluate(
  23. xpath, node, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  24.  
  25. var element = null;
  26. var i = 0, j;
  27.  
  28. while(element = result.snapshotItem(i++)){
  29.  
  30. // iframe or embed
  31. var url = element.src;
  32.  
  33. // object
  34. if(!url){
  35. for(j = 0; j < element.childNodes.length; j++){
  36. var pa = element.childNodes[j];
  37. if(pa.nodeName == "PARAM" && pa.getAttribute("name") == "movie"){
  38. url = pa.getAttribute("value");
  39. break;
  40. }
  41. }
  42. }
  43.  
  44. if(!url){
  45. continue;
  46. }
  47.  
  48. var id = url.match(/(embed|v)\/(.+?)(\?|&|$)/)[2];
  49. var a = document.createElement("a");
  50. var pageUrl = "http://www.youtube.com/watch?v=" + id;
  51. a.appendChild(document.createTextNode(pageUrl));
  52. a.setAttribute("href", pageUrl.replace("http:", ""));
  53. a.setAttribute("target", "_blank");
  54. a.className = "unembed";
  55.  
  56. element.parentNode.replaceChild(a, element);
  57. }
  58. };
  59.  
  60. new MutationObserver(function(mutations){
  61. if (document.body) {
  62. unEmbed(document.body);
  63. }
  64. }).observe(document, {
  65. childList: true,
  66. subtree: true
  67. });