Advertisement
Guest User

Silver Mitsuki - TBF Chat YT Player

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