No Embed Youtube

replace embed iframe, object with anchor link.

当前为 2017-04-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name No Embed Youtube
  3. // @version 2.2.0
  4. // @description replace embed iframe, object with anchor link.
  5. // @homepageURL https://github.com/eight04/no-embed-youtube
  6. // @supportURL https://github.com/eight04/no-embed-youtube/issues
  7. // @license MIT
  8. // @author eight04 <eight04@gmail.com>
  9. // @namespace eight04.blogspot.com
  10. // @include http*
  11. // @exclude http://www.youtube.com/*
  12. // @exclude https://www.youtube.com/*
  13. // @run-at document-start
  14. // @grant none
  15. // ==/UserScript==
  16.  
  17.  
  18. "use strict";
  19.  
  20. //http://www.cnet.com/news/youtubes-new-nocookie-feature-continues-to-serve-cookies/
  21.  
  22. var xpath = `(
  23. //iframe[
  24. contains(@src, 'youtube.com/embed/') or
  25. contains(@src, 'youtube.com/v/') or
  26. contains(@src, 'youtube-nocookie.com/embed/') or
  27. contains(@src, 'youtube-nocookie.com/v/') or
  28. contains(@data-src, 'youtube.com/embed/')
  29. ] |
  30. //object[./param[contains(@value, 'youtube.com/v/')]] |
  31. //embed[
  32. contains(@src, 'youtube.com/v/') and
  33. not(ancestor::object)
  34. ]
  35. )[not(ancestor::*[@id='YTLT-player'])]`;
  36.  
  37. var unEmbed = function(node){
  38.  
  39. var result = document.evaluate(
  40. xpath, node, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  41.  
  42. var element = null;
  43. var i = 0, j;
  44.  
  45. while ((element = result.snapshotItem(i++))) {
  46.  
  47. // iframe or embed
  48. var url = element.src || element.dataset.src;
  49.  
  50. // object
  51. if(!url){
  52. for(j = 0; j < element.childNodes.length; j++){
  53. var pa = element.childNodes[j];
  54. if(pa.nodeName == "PARAM" && pa.getAttribute("name") == "movie"){
  55. url = pa.getAttribute("value");
  56. break;
  57. }
  58. }
  59. }
  60.  
  61. if(!url){
  62. continue;
  63. }
  64.  
  65. // https://developers.google.com/youtube/player_parameters#Manual_IFrame_Embeds
  66. var id = url.match(/(embed|v)\/(.+?)(\?|&|$)/)[2];
  67. var query = url.match(/\?(.+)/);
  68. var a = document.createElement("a");
  69. var pageUrl = "//www.youtube.com/watch?v=" + id;
  70. a.textContent = "http:" + pageUrl;
  71. if (query) {
  72. pageUrl += "&" + query[1];
  73. }
  74. a.href = pageUrl;
  75. a.target = "_blank";
  76. a.className = "unembed";
  77.  
  78. element.parentNode.replaceChild(a, element);
  79. }
  80. };
  81.  
  82. new MutationObserver(function(){
  83. if (document.body) {
  84. unEmbed(document.body);
  85. }
  86. }).observe(document, {
  87. childList: true,
  88. subtree: true
  89. });