【问题标题】:Getting page to load completely before getting the content在获取内容之前让页面完全加载
【发布时间】:2018-08-21 17:38:20
【问题描述】:

我正在尝试使用 $(document).ready(function() 在使用 ajax 之前让页面完全加载,但由于某种原因它无法正常工作。我只能获取页面的一部分,因为在获取内容之前它不会等待它完全加载。

我可以修复代码的哪一部分来解决这个问题?

// ==UserScript==
// @name        test1
// @namespace   John Galt
// @description Basic Google Hello
// @match       *://www.google.com/*
// @version     1
// @require     http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant       GM_xmlhttpRequest
// ==/UserScript==

 $(document).ready(function(){

 GM_xmlhttpRequest ( {
    method: "GET",
    url:    "http://www.fictionpress.com/",
    onload: function (response) {
        var parser = new DOMParser ();
        var doc = parser.parseFromString (response.responseText, "text/html");
        var theString = new XMLSerializer().serializeToString(doc);
        document.write(theString);
    },
    onerror: function (e) {
        console.error ('**** error ', e);
    },
    onabort: function (e) {
        console.error ('**** abort ', e);
    },
    ontimeout: function (e) {
        console.error ('**** timeout ', e);
    }
} );

    });

【问题讨论】:

  • 您确定页面没有完全加载吗?可能有其他页面就绪代码执行进一步的 DOM 突变
  • @Ji_in_coding 是的,网站看起来只是部分加载。

标签: javascript userscripts tampermonkey document-ready gm-xmlhttprequest


【解决方案1】:

您可以添加一个选项: // @run-at document-end。这表示 Tampermonkey 您需要等待整页加载。

可以在here找到完整的选项列表。

【讨论】:

    猜你喜欢
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 2017-04-11
    • 1970-01-01
    相关资源
    最近更新 更多