Advertisement
Guest User

disable youtube integration in sidewise chrome extension

a guest
Jan 6th, 2015
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* content_script.js
  2. Copyright (c) 2012 Joel Thornton <sidewise@joelpt.net> See LICENSE.txt for license details. */
  3.  
  4. bfunction setUpTitleObserver(){var a=document.querySelector("head");a?(new window.WebKitMutationObserver(function(a){a=a[0];"attributes"==a.type&&"sidewise_event"==a.target.name?receivePageEvent(a.target):"childList"==a.type&&0<a.addedNodes.length&&"sidewise_event"==a.addedNodes[0].name?receivePageEvent(a.addedNodes[0]):notifySidewise()})).observe(a,{attributes:!0,subtree:!0,characterData:!0,childList:!0}):log("Page does not have head element")}
  5. function connectPort(){port=chrome.runtime.connect({name:"content_script"});log("connection",port);port.onMessage.addListener(function(a){log("message",a.op,a.action,a);switch(a.op){case "getPageDetails":sendPageDetails(a)}});port.onDisconnect.addListener(function(){log("disconnect",port)})}function onLocationOrHistoryChanged(a){log(a.type,a);notifySidewise()}
  6. function notifySidewise(){clearTimeout(notifyTimeout);notifyTimeout=setTimeout(function(){sendPageDetails({action:"store"})},MIN_WAIT_BETWEEN_NOTIFIES_MS)}
  7. function sendPageDetails(a){a.op="getPageDetails";a.title=document.title;a.referrer=document.referrer;a.historylength=history.length;a.sessionGuid=getSessionGuid();var b=document.querySelector("head > link[rel=icon], head > link[rel=favicon]");b&&(a.favicon=b.href);b=JSON.stringify(a);sessionStorage.sidewiseLastDetailsSent==b?log("skipping notify message send because details have not changed from last time they were sent"):(sessionStorage.sidewiseLastDetailsSent=b,log("pushing details via sendMessage",
  8. b),chrome.runtime.sendMessage(a))}function getSessionGuid(){var a=sessionStorage.sidewiseGuid;a||(a=generateGuid(),sessionStorage.sidewiseGuid=a);return a}function generateGuid(){var a=Math.random().toString(36);return a.substring(2,6)+"-"+a.substring(6,15)+"-"+Math.random().toString(36).substring(2,15)}function injectPageScript(a){var a="("+a+")();",b=document.createElement("script");b.textContent=a;(document.head||document.documentElement).appendChild(b);b.parentNode.removeChild(b)}
  9. function injectPageScriptSendEventFn(){injectPageScript(function(){window.sidewise_sendEvent=function(a,b){var c=document.createElement("meta");c.setAttribute("name","sidewise_event");c.setAttribute("event-name",a);c.setAttribute("event-value",b);document.head.appendChild(c)};window.sidewise_sendMediaUpdateEvent=function(a,b){window.sidewise_sendEvent("updateMediaState",a+","+b)}})}
  10. function receivePageEvent(a){var b=a.getAttribute("event-name"),c=a.getAttribute("event-value");a.parentElement.removeChild(a);switch(b){case "updateMediaState":a=c.split(",");chrome.runtime.sendMessage({op:"updateMediaState",state:MEDIA_PLAYER_STATE_ALIASES[a[0]]||a[0],time:parseFloat(a[1])});break;default:throw Error("Unrecognized event-name "+b);}}
  11. function setUpMediaMonitors(){injectPageScriptSendEventFn();/*injectYouTubeMonitoring();*/injectVimeoMonitoring();injectPageScript(jwPlayerEmbedScript);injectPageScript(html5VideoScript)}
  12. // function injectYouTubeMonitoring(){var a=null!==document.location.href.match(/https?:\/\/.*?youtube\..+?\//),b=null!==document.querySelector('iframe[src*="youtube."],embed[src*="youtube."]');if(a||b)injectPageScript(youTubeCommonScript),a?injectPageScript(youTubePageScript):setTimeout(function(){injectPageScript(youTubePlayerEmbedScript)},4E3)}
  13. // function youTubeCommonScript(){clearTimeout(window.sidewise_missedOnYoutubePlayerReadyTimer);clearInterval(window.sidewise_onVideoPlayingIntervalTimer);window.sidewise_onYouTubePlayerStateChange=function(a,b){var c,g;"number"==typeof a?(c=b||window.sidewise_ytplayer,g=a):(c=a.target,g=a.data);1==g?window.sidewise_onVideoPlayingIntervalTimer||(window.sidewise_onVideoPlayingIntervalTimer=setInterval(function(){window.sidewise_sendMediaUpdateEvent(g,c.getCurrentTime())},500)):(clearInterval(window.sidewise_onVideoPlayingIntervalTimer),
  14. // window.sidewise_onVideoPlayingIntervalTimer=null);window.sidewise_sendMediaUpdateEvent(g,c.getCurrentTime())}}
  15. // function youTubePageScript(){window.onYouTubePlayerReady=function(){window.sidewise_ytplayer||(window.sidewise_onVideoPlayingIntervalTimer=null,window.sidewise_missedOnYoutubePlayerReadyTimer=null,window.sidewise_ytplayer=document.getElementById("movie_player"),window.sidewise_ytplayer||(window.sidewise_ytplayer=document.getElementById("movie_player-flash")),window.sidewise_ytplayer?window.sidewise_ytplayer.addEventListener("onStateChange","sidewise_onYouTubePlayerStateChange"):window.sidewise_missedOnYouTubePlayerReadyTimer=
  16. // setTimeout(window.onYouTubePlayerReady,5E3))}}
  17. // function youTubePlayerEmbedScript(){window.getFrameId=function(a){var b=document.getElementById(a);if(b){if(/^iframe$/i.test(b.tagName))return a;b=b.getElementsByTagName("iframe");if(!b.length)return null;for(var c=0;c<b.length&&!/^https?:\/\/(?:www\.)?youtube(?:-nocookie)?\.com(\/|$)/i.test(b[c].src);c++);b=b[c];if(b.id)return b.id;do a+="-frame";while(document.getElementById(a));return b.id=a}return null};var a,b=[],c=!1;a=function(a,f){if(!0===a){c=!0;for(var e=0;e<b.length;e++)b.shift()()}else if("function"==
  18. // typeof a)if(c)a();else b[f?"unshift":"push"](a)};window.onYouTubePlayerAPIReady=function(){a(!0)};var g=document.querySelectorAll('iframe[src*="youtube."]'),d=document.querySelectorAll('embed[src*="youtube."]');if(!(0==g.length&&0==d.length)){var i=document.createElement("script");i.src="http://www.youtube.com/player_api";var e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(i,e);var j=function(a){return function(b){window.sidewise_onYouTubePlayerStateChange(b,a)}};a(function(){for(var a=
  19. // 0;a<g.length;a++){var b=g[a];b.id||(b.id=Math.random().toString(26).slice(2));(b=getFrameId(b.id))&&new YT.Player(b,{events:{onStateChange:sidewise_onYouTubePlayerStateChange}})}for(a=0;a<d.length;a++){b=d[a];b.id||(b.id=Math.random().toString(26).slice(2));var c="sidewise_onYouTubePlayerStateChange_"+b.id;window[c]=j(b);d[a].addEventListener("onStateChange",c)}})}}
  20. function jwPlayerEmbedScript(){"function"==typeof jwplayer&&(window.sidewise_onJwPlayerCheck=function(){var a=jwplayer();if(a){try{var b=a.getState()}catch(c){return}if(b&&(b=b.toLowerCase(),a=a.getPosition(),"playing"==b||b!=window.sidewise_jwPlayerLastState))window.sidewise_sendMediaUpdateEvent(b,a),window.sidewise_jwPlayerLastState=b}},window.sidewise_onJwPlayerCheckInterval&&clearInterval(window.sidewise_onJwPlayerCheckInterval),window.sidewise_onJwPlayerCheckInterval=setInterval(window.sidewise_onJwPlayerCheck,
  21. 500))}var vimeoPageCheckInterval=null;
  22. function injectVimeoMonitoring(){document.location.href.match(/https?:\/\/.*?vimeo\..+?\//)?(clearInterval(vimeoPageCheckInterval),vimeoPageCheckInterval=setInterval(function(){document.querySelector("object[type*=flash][data*=moogaloop]")&&(clearInterval(vimeoPageCheckInterval),injectPageScript(vimeoPageScript))},1E3),setTimeout(function(){clearInterval(vimeoPageCheckInterval)},2E4)):document.querySelector('iframe[src*="player.vimeo."]')&&injectPageScript(vimeoPlayerEmbedScript)}
  23. function vimeoPageScript(){var a=document.querySelectorAll("object[type*=flash][data*=moogaloop]");0!=a.length&&(window.sidewise_onVimeoPagePause=function(){window.sidewise_sendMediaUpdateEvent("paused",0)},window.sidewise_onVimeoPageProgress=function(a){"number"==typeof a?window.sidewise_sendMediaUpdateEvent("playing",a):window.sidewise_sendMediaUpdateEvent("playing",a.seconds)},setTimeout(function(){for(var b=0;b<a.length;b++){var c=a[b];c.api_addEventListener("onProgress","sidewise_onVimeoPageProgress");
  24. c.api_addEventListener("onPause","sidewise_onVimeoPagePause")}},2E3))}
  25. function vimeoPlayerEmbedScript(){var a=document.querySelectorAll('iframe[src*="player.vimeo."]');if(0!=a.length){var b,c=function(a){return new c.fn.init(a)},g=function(a,b,k){if(!k.contentWindow.postMessage)return!1;var c=k.getAttribute("src").split("?")[0],a=JSON.stringify({method:a,value:b});"//"===c.substr(0,2)&&(c=window.location.protocol+c);k.contentWindow.postMessage(a,c)},d=function(a){var b,c;try{b=JSON.parse(a.data),c=b.event||b.method}catch(g){}"ready"==c&&!j&&(j=!0);if(a.origin!=m)return!1;
  26. var a=b.value,f=b.data,d=""===d?null:b.player_id;b=d?e[d][c]:e[c];c=[];if(!b)return!1;void 0!==a&&c.push(a);f&&c.push(f);d&&c.push(d);return 0<c.length?b.apply(null,c):b.call()},i=function(a,b,c){c?(e[c]||(e[c]={}),e[c][a]=b):e[a]=b},e={},j=!1,m="";c.fn=c.prototype={element:null,init:function(a){"string"===typeof a&&(a=document.getElementById(a));this.element=a;a=this.element.getAttribute("src");"//"===a.substr(0,2)&&(a=window.location.protocol+a);for(var a=a.split("/"),b="",c=0,d=a.length;c<d;c++){if(3>
  27. c)b+=a[c];else break;2>c&&(b+="/")}m=b;return this},api:function(a,b){if(!this.element||!a)return!1;var c=this.element,d=""!==c.id?c.id:null,e=!b||!b.constructor||!b.call||!b.apply?b:null,f=b&&b.constructor&&b.call&&b.apply?b:null;f&&i(a,f,d);g(a,e,c);return this},addEvent:function(a,b){if(!this.element)return!1;var c=this.element,d=""!==c.id?c.id:null;i(a,b,d);"ready"!=a?g("addEventListener",a,c):"ready"==a&&j&&b.call(null,d);return this},removeEvent:function(a){if(!this.element)return!1;var b=this.element,
  28. c;a:{if((c=""!==b.id?b.id:null)&&e[c]){if(!e[c][a]){c=!1;break a}e[c][a]=null}else{if(!e[a]){c=!1;break a}e[a]=null}c=!0}"ready"!=a&&c&&g("removeEventListener",a,b)}};c.fn.init.prototype=c.fn;window.addEventListener?window.addEventListener("message",d,!1):window.attachEvent("onmessage",d);b=window.Froogaloop=window.$f=c;window.sidewise_onVimeoPlayProgress=function(a){window.sidewise_sendMediaUpdateEvent("playing",a.seconds)};window.sidewise_onVimeoPause=function(){window.sidewise_sendMediaUpdateEvent("paused",
  29. 0)};window.sidewise_onVimeoPlayerReady=function(a){b(a).addEvent("playProgress",window.sidewise_onVimeoPlayProgress);b(a).addEvent("pause",window.sidewise_onVimeoPause)};for(d=0;d<a.length;d++){var f=a[d];f.id||(f.id=Math.random().toString(26).slice(2));var h=f.src,l=0<=f.src.indexOf("?")?"&":"?";-1==f.src.indexOf(/[\?\&]api=1/)&&(h+=l+"api=1",l="&");-1==f.src.indexOf(/[\?\&]player_id=/)&&(h+=l+"player_id="+f.id);f.src!=h&&(f.src=h);b(f).addEvent("ready",window.sidewise_onVimeoPlayerReady)}}}
  30. function html5VideoScript(){window.sidewise_html5videos=document.querySelectorAll("video");if(0==window.sidewise_html5videos.length)window.sidewise_html5videosRetried||(window.sidewise_html5videosRetried=!0,setTimeout(html5VideoScript,100));else{window.sidewise_onHtml5VideoProgress=function(a){a=a.target;window.sidewise_sendMediaUpdateEvent(a.paused?"paused":"playing",a.currentTime)};for(var a=0;a<window.sidewise_html5videos.length;a++){var b=window.sidewise_html5videos[a];b.addEventListener("playing",
  31. window.sidewise_onHtml5VideoProgress);b.addEventListener("progress",window.sidewise_onHtml5VideoProgress);b.addEventListener("pause",window.sidewise_onHtml5VideoProgress)}}}function log(){LOGGING_ENABLED&&console.log.apply(console,arguments)};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement