How to write userscript for a dynamic website [duplicate]

丶灬走出姿态 提交于 2019-12-20 06:47:12

问题


**First of all, i'm sorry for my bad english!

I am trying to write a userscript for some website. This website updates himself every click or some seconds. (it uses ajax to update the view without refreshing) My userscript have to append some html elements when the user is in certain view. How can i run my code every view change? (after ajax)

The page have a function called ajaxCall that sends an ajax request and changes the page according to the response. I tried something like this:

var source = window.ajaxCall;
window.ajaxCall = function(param){ 
    source(param);
    myFunc();
}

this didn't work because ajaxCall sends async ajax request, so: first, the source function called (ajax request started), and immediately after it, my function called. (before the ajax request succeed and view changed)

Is there a way to run my code immediately after page updates?

Thank you very much.


回答1:


try to use JQuery Library

Example 1:


 $.ajax({
  url: "http://fiddle.jshell.net/favicon.png",
  beforeSend: function( xhr ) {
    xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
  }
})
  .done(function( data ) {
    if ( console && console.log ) {
      console.log( "Sample of data:", data.slice( 0, 100 ) );
    }
  });

example 2:

    var jqxhr = $.ajax( "example.php" )
      .done(function() {
        alert( "success" );
      })
      .fail(function() {
        alert( "error" );
      })
      .always(function() {
        alert( "complete" );
      });

    // Perform other work here ...

    // Set another completion function for the request above
    jqxhr.always(function() {
      alert( "second complete" );
    });


来源:https://stackoverflow.com/questions/29763856/how-to-write-userscript-for-a-dynamic-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!