Advertisement
Guest User

[TBF] Youtube Player on top of the Chat 0.2.1

a guest
Mar 10th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==================================================
  2. // #    [terrabattleforum.com - Silver Mitsuki]     #
  3. // ==================================================
  4. //
  5. // Some easy code that adds a youtube player on top of the TBF chat
  6. // doesn't add anything when using the pupup chat (/chat/popup)
  7.  
  8. // Changelog
  9. // [19 Feb 2017]    First version, let's see how it goes
  10. // [3 March 2017]   Improved regex, better customization, fixed known
  11. //                  'a' tag issue on FF; still have some work to do.
  12. // [10 March 2017]  A very quick update that addresses a little problem
  13.  
  14. // Known issue (at release)
  15. // None, but I didn't have time to add a couple of more features.
  16.  
  17. // FAQ
  18. // Q: How does this script work?
  19. // A: I often update the MOTD on top of the chat with announcements
  20. // and such. This script simply looks at the links posted there and
  21. // appends a small player on top of the chat for every video found
  22. // written with a specific format (=> not every video adds a player).
  23.  
  24. // Q: Where do I find newer and older versions?
  25. // A: Somewhere here: http://terrabattleforum.com/members/4454/
  26.  
  27. // Q: How do I install it?
  28. // A: Download an extension that allows the user to load a custom JS
  29. // code, like "Custom JavaScript for websites" ( https://goo.gl/Umvo1Q )
  30. // for Chrome and "JS Injector" ( https://goo.gl/6ojztF ) for Firefox.
  31. // Once installed, you'll see a button on the top right part of the
  32. // window, near the Options menu. Click on it, paste this code,
  33. // and click on Save (if there is no button, look for the list of all
  34. // the installed extensions);
  35. // If a URL is required, write http://terrabattleforum.com/
  36. // The script can be paused through the same menu, by disabling
  37. // the extension or by toggling the value below.
  38.  
  39. // config
  40. // feel free to change the following values; use "true" and "false"
  41. // (without quotes) to enable / disable each option.
  42.  
  43. // toggle to disable this script
  44. var s_enableScript = true;
  45.  
  46. // The player will be placed accordingly to the 's_playerType' value.
  47. //
  48. //      0   =   Above Chat, small
  49. //      1   =   Above Chat, big
  50. //      2   =   Below Chat, small
  51. //      3   =   Below Chat, big
  52. //      4   =   On Right of the chat, very small
  53. //      5   =   [in development] Background of the chat. Don't ask me why.
  54. //
  55. var s_playerType = 2;
  56.  
  57. // height, in pixels, of the small (0 and 2) player. Default is 300
  58. var s_smallPlayerHeight = 300;
  59.  
  60. // other options
  61. var s_allowFullscreen   = true;     // fullscreen button selectable
  62. var s_autoPlay          = true;     // toggle autoplay
  63.  
  64. //var s_allowAudio      = true;     // [in development] toggle mute/audio
  65.  
  66. // ###################################################################
  67. // ###################################################################
  68.  
  69. // Avoid to touch anything from this point on,
  70. // unless you know what you're doing
  71.  
  72. // man, this is hard, what could it be?
  73. var s_DEBUG = false;
  74.  
  75. // for development purposes, prints a video without checking che MOTD
  76. var s_useSimulatedURL   = false;
  77. var s_countSimulatedURL = 1;
  78.  
  79. // sizing constants
  80. var PLAYER_SIZE_SHRINK  = 0;
  81. var PLAYER_SIZE_ADAPT   = 1;
  82.  
  83. // positioning constants
  84. var PLAYER_POS_ABOVE_SMALL  = 0;
  85. var PLAYER_POS_ABOVE_BIG    = 1;
  86. var PLAYER_POS_BELOW_SMALL  = 2;
  87. var PLAYER_POS_BELOW_BIG    = 3;
  88. var PLAYER_POS_SIDE_MINI    = 4;
  89. var PLAYER_POS_BG_CHAT      = 5; // toDo
  90.  
  91. // pattern for recognizing shortened video(s)
  92. // (again, I don't need every possible pattern, just a small set)
  93. var s_match = (s_DEBUG)
  94.     ?   "^(?:.*\?v=)(.[^\/:]+)$"
  95.     :   "^(?:https?:\/\/)?(?:youtu.be\/)(.[^\/:]+)$";
  96.  
  97. // other stuff
  98. // main chat container
  99. var s_main = document.getElementById('taigachat_full');
  100. var s_player = null;
  101.  
  102. // #################################################################
  103. // and here we go
  104. // =================================================================
  105. // Might not work anymore after some changes to the forum's template,
  106. // (I refer to few ids), but is more efficient than checking for all
  107. // of the existing tags, looking for those I need. no jQuery.
  108. // #################################################################
  109.  
  110. // Someone (*coff* Firefox) requires to have a fully loaded content
  111. // at its disposal in order to retrieve the links
  112. var callback = function() {
  113.    
  114.    // Script disabled
  115.     if (!s_enableScript)
  116.         return false;
  117.    
  118.    // Chat Yes, Forum No
  119.     var tagList = document.getElementById('taigachat_motd');
  120.     if (tagList === null)
  121.         return false;
  122.        
  123.     // !popup only
  124.     if (s_hasClass(s_main, "taigachat_popup"))
  125.         return false;
  126.    
  127.     // development purposes
  128.     if (s_useSimulatedURL){
  129.         for (var c = 0; c < s_countSimulatedURL; c++)
  130.             s_appendEmbed("rV22wrQl16I");
  131.         return true;
  132.     }
  133.    
  134.     // checks links for youtube videos
  135.     // note: Firefox has some problems with
  136.     tagList = tagList.getElementsByTagName("a");
  137.     var regex = new RegExp(s_match);
  138.     var url, match;
  139.    
  140.     // retrieves youtube URLs from MOTD
  141.     // (I use other hostnames for regular videos, like goo.gl)
  142.     for (var i = 0, tagList_count=tagList.length; i < tagList_count; i++)
  143.         if ((tagList[i] !== "undefined")
  144.             && (tagList[i].hasAttribute("href"))){
  145.             url = tagList[i].getAttribute("href");
  146.            
  147.             match = url.match(regex);
  148.             // testing purposes
  149.             if (s_DEBUG){
  150.                 console.log(url);
  151.                 console.log(match);
  152.             }
  153.            
  154.             if (match && match[1] !== "undefined")
  155.                 s_appendEmbed(match[1]);
  156.             else if(s_DEBUG)
  157.                 console.log("^nope");
  158.            
  159.         }
  160.        
  161.     return true;
  162. };
  163.  
  164. // calls the main function
  165. if (
  166.     document.readyState === "complete" ||
  167.     (document.readyState !== "loading" && !document.documentElement.doScroll)
  168. ) {
  169.   callback();
  170. } else {
  171.   document.addEventListener("DOMContentLoaded", callback);
  172. }
  173.  
  174. // return true if 'el' has the 'className' CSS class
  175. // jQuery .hasClass() can be used instead (TBF uses 1.11)
  176. function s_hasClass(el, className) {
  177.    
  178.     className = " " + className + " ";
  179.     var replace = (" " + el.className + " ").replace(/[\n\t]/g, " ").indexOf(className);
  180.     return (replace > -1);
  181. }
  182.  
  183. // append a node after another one
  184. function s_insertAfter(newNode, referenceNode) {
  185.     referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
  186. }
  187.  
  188. // generates the youtube video player to append
  189. function s_appendEmbed(videoId){
  190.    
  191.     // container
  192.     var elem = document.createElement('div');
  193.     elem.id = "ytCont-"+videoId;
  194.    
  195.     //elem.id="s_embed_player_"+loop;
  196.     var elemAttr = "position: relative; width: 100%; margin-bottom:10px;";
  197.     elemAttr += (s_getPlayerSize() === PLAYER_SIZE_SHRINK
  198.                 && s_smallPlayerHeight > 0)
  199.                 ? " height: "+s_smallPlayerHeight+"px;"
  200.                 : " height: 0; padding-bottom: 56.25%;";
  201.    
  202.     elem.setAttribute("style", elemAttr);
  203.    
  204.     // element
  205.     var elem2 = document.createElement('iframe');
  206.     elem2.id = "ytPlayer-"+videoId;
  207.    
  208.     // CSS Style
  209.     elemAttr = "position: absolute; top: 0; left: 0;";
  210.     elemAttr += " width: 100%; height: 100%;";
  211.     if (s_getPlayerSize() === PLAYER_SIZE_SHRINK
  212.         && s_smallPlayerHeight > 0)
  213.         elemAttr += " max-height: "+s_smallPlayerHeight+"px;";
  214.    
  215.     elem2.setAttribute("style", elemAttr);
  216.    
  217.     // SRC
  218.     elemAttr = "http://www.youtube.com/embed/"+videoId;
  219.     if (s_autoPlay)
  220.     elemAttr += "?autoplay=1";
  221.    
  222.     elem2.setAttribute("src", elemAttr);
  223.    
  224.     // General
  225.     elem2.setAttribute("type", "text/html");
  226.     elem2.setAttribute("allowTransparency", "true");
  227.     elem2.setAttribute("frameborder", "0");
  228.    
  229.     // fullscreen
  230.     if (s_allowFullscreen){
  231.         elem2.setAttribute("allowfullscreen", "1");
  232.         // legacy
  233.         elem2.setAttribute("webkitallowfullscreen", "1");
  234.         elem2.setAttribute("mozallowfullscreen", "1");
  235.     }
  236.    
  237.     elem.appendChild(elem2);
  238.    
  239.     // and here we go
  240.     s_appendPlayer(elem);
  241.    
  242.     // toDo
  243.     //if (!s_allowAudio){
  244.    
  245.         //var script = document.createElement('script');
  246.         //script.src = "https://www.youtube.com/iframe_api";
  247.        
  248.         //var s_mainCont = document.getElementById("content");
  249.         //s_mainCont.insertBefore(script, s_mainCont.firstChild);
  250.        
  251.         //elem2.setAttribute("muted", "muted");
  252.     //}
  253.    
  254.     return true;
  255. }
  256.  
  257. // actually appends elem to the webpage
  258. function s_appendPlayer(elem){
  259.    
  260.     // container 2, the revenge
  261.     switch(s_playerType){
  262.        
  263.         case PLAYER_POS_BELOW_SMALL:
  264.         case PLAYER_POS_BELOW_BIG:
  265.            
  266.             if (s_player === null)
  267.                 s_player = s_main;
  268.                
  269.             s_player.appendChild(elem);
  270.             break;
  271.            
  272.         //case PLAYER_POS_BG_CHAT:
  273.            
  274.         //    if (s_player === null)
  275.         //        s_player = document.getElementById("taigachat_box");
  276.                
  277.         //    s_player.appendChild(elem);
  278.            
  279.         //    break;
  280.            
  281.         case PLAYER_POS_SIDE_MINI:
  282.            
  283.             if (s_player === null){
  284.                
  285.                 var newElem = document.createElement('div');
  286.                 newElem.setAttribute("class", "section");
  287.                
  288.                 var newElem2 = document.createElement('div');
  289.                 newElem2.id = "widget-99";
  290.                 newElem2.setAttribute("class", "secondaryContent");
  291.                
  292.                 var newElem3 = document.createElement('h3');
  293.                 newElem3.innerHTML = "Upcoming Streams";
  294.                
  295.                 // #section-99.secondaryContent h3
  296.                 newElem2.appendChild(newElem3);
  297.                
  298.                 // .section #section-99.secondaryContent
  299.                 newElem.appendChild(newElem2);
  300.                
  301.                 // #{{mainCont}} + .section #section-99.secondaryContent
  302.                 var mainCont = document.getElementById("widget-5");
  303.                 if (mainCont === null)
  304.                     mainCont = document.getElementById("taigachat_online_users_holder");
  305.                
  306.                 s_insertAfter(newElem, mainCont);
  307.                
  308.                 s_player = newElem2;
  309.             }
  310.                
  311.             s_player.appendChild(elem);
  312.             break;
  313.        
  314.         default:
  315.         //case PLAYER_POS_ABOVE_SMALL:
  316.         //case PLAYER_POS_ABOVE_BIG:
  317.            
  318.             if (s_player === null)
  319.             s_player = (s_hasClass(s_main.children[0], "primaryContent"))
  320.                     ? s_main.children[0]
  321.                     : s_main.children[0].children[0];
  322.            
  323.             s_player.insertBefore(elem, s_player.firstChild);
  324.             break;
  325.     }
  326.    
  327.     return s_main;
  328. }
  329.  
  330. // returns the size of the video player
  331. function s_getPlayerSize(){
  332.    
  333.     switch(s_playerType){
  334.        
  335.         case PLAYER_POS_ABOVE_SMALL:
  336.         case PLAYER_POS_BELOW_SMALL:
  337.             return PLAYER_SIZE_SHRINK;
  338.             break;
  339.     }
  340.    
  341.     return PLAYER_SIZE_ADAPT;
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement