前提・実現したいこと
youtubeで特定のチャンネルだけ見れるようにしたく、Tampermonkey(Javascript)でyoutubeのチャンネル名を取得し、それがホワイトリストのものと適合しなかったらウインドウを閉じる(見えなくする)というコードを動かそうとしています。
tampermonkeyのコードが転がっていたので流用したかったのですが、正しく動きません。
発生している問題・エラーメッセージ
下記のコードで、
var testString = $(".yt-user-info")[0].firstElementChild.innerHTML;
が機能せず、そこでコードが止まってしまいます(特にエラーなどなし)
該当のソースコード
// ==UserScript==
// @name Youtube Whitelist Script
// @namespace http://tampermonkey.net/
// @version 0.1b
// @description This script will remove the content from Youtube videos. Ignores whitelisted content
// @author Louis Vaught
// @match https://www.youtube.com/watch*
// @copyright 2017, Louis Vaught
// @require https://code.jquery.com/jquery-latest.js
// ==/UserScript==
var $ = window.jQuery;
///////////////////////
// EDIT WHITELIST HERE:
///////////////////////
var whitelist = [
'TheBackyardScientist',
'Cody\'sLab',
'The Slo Mo Guys'
];
//Make sure to match the name exactly. Not case-sensitive.
//////////////////////////
// String Search Function:
//////////////////////////
function searchStringInArray (str, strArray) {
for (var j=0; j<strArray.length; j++) {
var testStr = strArray[j].toLowerCase();
var matchStr = str.toLowerCase();
if (testStr.match(matchStr)) return j;
}
return -1;
}
////////////////////
// Main Script Body:
////////////////////
$(document).ready(function() {
alert("main kokokara")
//Find the channel name on the page
var testString = $(".yt-user-info")[0].firstElementChild.innerHTML;
// var testString ="hogehoge"
alert(testString)
//Search for the channel name in the array
var searchVal = searchStringInArray(testString,whitelist);
//If the channel isn't present, then delete content and error out:
if (searchVal==-1) {
$("#page-container").remove();
alert("This channel isn't on the Youtube whitelist!");
}
});
試したこと
コードの上部で
// @require http://code.jquery.com/jquery-latest.js
var $ = window.jQuery;
と指定しているのでjQueryは使えると思うんですが、(エラーはでない)
実際には使えていないのかもしれません。
コードの間違い、もしくはjQueryの使い方の間違いなどがありましたらご指摘いただけますと嬉しいです。
よろしくお願いいたします。