Chrome扩展程序 - SoundCloud页面操作

时间:2014-08-26 02:27:28

标签: javascript google-chrome google-chrome-extension google-chrome-devtools

我正在构建一个页面操作chrome扩展,当我在SoundCloud上操作内容时,我遇到了问题。我希望我的分机能够分析页面上每首歌曲的播放,喜欢,转发和评论,然后根据数据向Feed中的每首歌曲添加其他信息。

问题是当页面加载时,没有声音加载到feed中,而是java脚本在页面加载后加载它们。因此,当我在加载时阅读带扩展名的文档时,没有数据可供阅读。最初我通过添加从页面加载到内容脚本运行时的延迟来解决这个问题,但是当用户向下滚动并加载更多歌曲时,这不起作用。

有没有人对我可以用作运行内容脚本的指标有任何想法。换句话说,我可以使用什么提示来确定在页面上有新歌要分析。

由于

1 个答案:

答案 0 :(得分:0)

我做了一些类似的事情,我找到的解决方案是创建自己的onElementInsert函数并不断检查,直到内容注入页面。

我已经改变了我的代码以尝试满足您的需求。您可能需要进行其他更改:

function onElementInsert( parent, func ) {
    var startingElementCount = parent.childNodes.length;

    // Checks to see if the element's number of immediate children nodes increased
    var checkChildCount = function(count) {
        if ( count < parent.childNodes.length ) {
            func();
            return true;
        }
        return false;
    }

    // Interval function to repeat every 100 milliseconds
    var interval = window.setInterval( function() {
        // Check if there are any new children
        if ( checkChildCount(startingElementCount) ) {
            // If so then trigger the insert event
            document.dispatchEvent( new Event('insertEvent') );
        }
    }, 100 );

    // Add an event listener for our insert event which will tell our interval to stop
    document.addEventListener('insertEvent', function (e) {
        clearInterval(interval);
    }, false); 
}

onElementInsert( document.getElementsByClassName('lazyLoadingList')[0].childNodes[0], function() {
   // Enter code here to execute when content has been added
});

所以这里发生了什么:

  1. 此代码调用onElementInsert(ele,func)函数,该函数将持续检查是否有任何新子节点添加到传递给它的元素(ele)中。
  2. 当插入发生时,它将执行函数(func)和 将触发&#39; insertEvent&#39;这会告诉函数停止 检查进一步插入。
  3. 具有类名&#39; lazyLoadingList&#39;的元素是我发现SoundCloud将它的soundList项加载到的元素。可能想亲自检查一下。

    另一件要注意的事情是,这只会检查你传递的元素的新直接孩子,它不会检测到孩子的孩子。因此,当您使用它时,请确保在插入内容的元素上调用它 - 或者更改它以检查所有子项。