2

Pandora AJAX 不适用于 waitForKeyElements。我花了几个小时试图找出我哪里出错了,但无法解决。

它工作一次,但在播放下一首曲目时不起作用。

// ==UserScript==
// @name         Pandora I am listening to
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Pandora I am listening to
// @author       You
// @match        http*://www.pandora.com/*
// @include      https://www.pandora.com/station/play/*
// @require      https://gist.githubusercontent.com/BrockA/2625891/raw/9c97aa67ff9c5d56be34a55ad6c18a314e5eb548/waitForKeyElements.js
// @require     http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js
// @grant    GM_addStyle
// ==/UserScript==

function liveNow() {
    setTimeout(function() {
        console.log(`Now Playing ${$(".songTitle")[0].innerHTML}`);
    }, 6000);  // delay for loading of music

}

waitForKeyElements ("#trackInfoContainer .songTitle", liveNow);
4

1 回答 1

3

#trackInfoContainer正在为每个额外的轨道重复使用(而不是重新创建)。

因此,等待节点创建是不够的,您还必须检查内容是否已更改。
以下是使用 waitForKeyElements 的方法:

waitForKeyElements ("#trackInfoContainer .songTitle", liveNow);

function liveNow (jNode) {
    var titleText  = jNode.text ().trim ();
    var lastText   = jNode.data ("lastText")  ||  "";

    if (titleText != lastText) {
        jNode.data ("lastText", titleText);
        console.log (`Now Playing ${titleText}`);
    }
    return true;  //-- Tell waitForKeyElements to keep checking this node.
}
于 2017-01-09T06:20:00.577 回答