From ab2005e27e3035d6c74af341a544ef22e8e799c4 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 1 Jun 2016 22:45:42 +0200 Subject: [PATCH] Added experimental Twitch support --- MLButton.user.js | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/MLButton.user.js b/MLButton.user.js index ca42534..db8896e 100644 --- a/MLButton.user.js +++ b/MLButton.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name MonsterLyrics button // @namespace https://github.com/NorbiPeti/ -// @version 0.3 +// @version 0.4 // @description Creates a button that searches for a lyrics video! // @author NorbiPeti // @match https://www.youtube.com/ @@ -11,36 +11,56 @@ // @grant none // ==/UserScript== -window.lasturl=""; window.lasttitle=""; window.addtries=0; +window.twitchmsg=""; function Add() { - var title=document.getElementById("eow-title").title; - if(window.location.href==window.lasturl) + var title=document.getElementById("eow-title"); + if(title!==null) + title=title.title; + if(document.getElementById("mlbutton")!==null) return; - if(title==lasttitle && window.lasttries<10) + if(window.location.href.indexOf("youtube.com")!=-1 && title==lasttitle && window.lasttries<10) { window.addtries++; window.setTimeout(Add, 100); return; } var cont=document.getElementById("watch7-subscription-container"); + if(cont===null) + cont=window.twitchmsg; if(cont===null) return; - cont.innerHTML+=""; - window.lasturl=window.location.href; //TODO: Show Lyrics button + window.lasttitle=window.twitchmsg.innerHTML.replace("Now Playing: ", "").replace(/ \- Listen now: .+/g, ""); + cont.innerHTML+=""; + console.log("!"); + //window.lasturl=window.location.href; //TODO: Show Lyrics button } function AddIfChanged() { var namecont=document.getElementById("watch7-user-header"); - if(namecont===null || namecont.innerHTML.indexOf("Monstercat")==-1) + var namecontTwitch=document.getElementsByClassName("chat-line"); + var Twitch=null; + for(var i=namecontTwitch.length-1; i>=0; i--) //Reminder: Don't put i++ in a supposedly decrementing loop... It's hard to debug + { + if(typeof namecontTwitch[i] != 'undefined' && namecontTwitch[i].getElementsByClassName("from")[0].innerHTML=="Monstercat") + { + Twitch=namecontTwitch[i].getElementsByClassName("message")[0]; + break; + } + } + if((namecont===null || namecont.innerHTML.indexOf("Monstercat")==-1) && (Twitch===null)) return; if(window.location.href==window.lasturl) return; + if(document.getElementById("mlbutton")!==null) + return; + console.log("Twitch"); + window.twitchmsg=Twitch; var cont=document.getElementById("watch7-subscription-container"); - if(cont===null) + if(cont===null && Twitch===null) return; window.setTimeout(Add, 100); } //TODO: Add for every platform, not just YouTube @@ -48,14 +68,14 @@ function AddIfChanged() (function() { 'use strict'; - // Your code here... AddIfChanged(); window.setInterval(AddIfChanged, 1000); })(); window.showLyrics=function() { - var title=document.getElementById("eow-title").title; + //var title=document.getElementById("eow-title").title; + var title=window.lastitle; title=encodeURI(title.replace(/\[[^\[\]]+\]/g, "")).replace("&", ""); //Tested on: Pegboard Nerds & NGHTMRE - Superstar (ft. Krewella) window.location.href="https://www.youtube.com/user/monstercatmedialyric/search?query="+title; };