function getElsByClass(searchClass,node) {
if ( node == null )
node = document;
var classElements = [],
els = node.getElementsByTagName("div"),
elsLen = els.length,
pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"), i, j;
for (i = 0, j = 0; i < elsLen; i++) {
if ( pattern.test(els[i].className) ) {
classElements[j] = els[i];
j++;
}
}
return classElements;
}
function getSong() {
console.log("getSong ran");
var songN = getElsByClass("info")[0], nmSong = getCookie();
console.log(songN);
songN = songN.getElementsByTagName("a");
songN = songN[0].innerText + " - " + songN[1].innerText;
if (nmSong != songN) {
setCookie(songN);
} else {
setCookie(songN);
}
sendSong(songN);
return songN;
}
setInterval(getSong(), 10000);
我已经尝试了songN = document.getElemenetsByClassName("info")
,我找到的每一个组合,它只会在关闭前运行30秒,我需要它运行几个小时,无人看管......
我可以跑
var songN = getElsByClass("info")[0];
console.log(songN); //is actually defined when ran in Javascript Console in Chrome..
songN = songN.getElementsByTagName("a");
songN = songN[0].innerText + " - " + songN[1].innerText;
在Javascript控制台中,它准确地返回,我正在尝试做什么,但如果它作为用户脚本运行或者在它被注入网页之后,我得到Uncaught TypeError: Cannot read property 'getElementsByTagName' of undefined
,我已经尝试过了通过搜索google和stackoverflow找到了很多不同的方法,我在<head>
的window.load上运行了脚本,并试图让它在<body>
中运行。
getElsByClass()
是我最后一次尝试它,它实际上并不是我的脚本的一部分,只是我尝试从另一个脚本,尝试缩短名称,以便它不会发生冲突,但仍然没有。
===更新===
以下是一些代码..
function getCookie() {
console.log("getCookie ran");
var sname = "songname=", ca = document.cookie.split(';'), i = 0, c, t;
for (i; i < ca.length; i++) {
c = ca[i].trim();
if (c.indexOf(sname) == 0) {
t = c.substring(sname.length, c.length);
console.log(t);
return t;
}
}
return "";
}
function setCookie(cvalue) {
console.log("setCookie ran");
var d = new Date();
d.setTime(d.getTime() + 210000);//set it to expire in 3 minutes and 30 seconds from current time (this is average song time.)
document.cookie = "songname=" + cvalue + "; expires=" + d.toGMTString();
}