chrome 中的 Tampermonkey 调用延迟加载锚标记

问题描述 投票:0回答:0

我希望脚本在任何已将其加载到 imdb 的页面上单击家长指南锚链接。 找到 waitForKeyElements 的代码。它触发但 TargetLink.length = 0 因为父指南部分在触发时尚未加载。 我的脚本如下

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://www.imdb.com/title/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// @run-at      document-idle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

(function() {
    waitForKeyElements (
        "a:contains('Parents guide'))"
        , clickWhen());
})();

function clickWhen () {
  //--- Note that the contains() text is case-sensitive.
        var TargetLink = $("a:contains('Parents guide')")
        if (TargetLink.length)
            window.location.href = TargetLink[0].href
}

我正在测试的页面链接 https://www.imdb.com/title/tt1431045/

测试页代码

<a class="ipc-metadata-list-item__icon-link" role="button" tabindex="0" aria-label="Parents guide: see all" aria-disabled="false" href="/title/tt1431045/parentalguide?ref_=tt_stry_pg"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" class="ipc-icon ipc-icon--chevron-right" id="iconContext-chevron-right" viewBox="0 0 24 24" fill="currentColor" role="presentation"><path fill="none" d="M0 0h24v24H0V0z"></path><path d="M9.29 6.71a.996.996 0 0 0 0 1.41L13.17 12l-3.88 3.88a.996.996 0 1 0 1.41 1.41l4.59-4.59a.996.996 0 0 0 0-1.41L10.7 6.7c-.38-.38-1.02-.38-1.41.01z"></path></svg></a>

调试截图。您可以看到页面尚未完全加载家长指南部分

jquery google-chrome tampermonkey
© www.soinside.com 2019 - 2024. All rights reserved.