猿问

关于AJAX请求的Fire Greasemonkey脚本

我正在处理用户脚本,我刚刚发现当主页面发出AJAX请求时脚本没有运行。

有没有办法在主页加载和AJAX请求上触发用户脚本?


GCT1015
浏览 440回答 2
2回答

牧羊人nacy

另一种方法 - 更简单,更小但更不灵活 - 是使用JavaScript时间延迟来等待AJAX / jQuery加载和完成。例如,如果在首次加载后动态生成以下HTML:<div id="userBlather">&nbsp; &nbsp; <div class="comment"> Comment 1... </div>&nbsp;&nbsp; &nbsp; <div class="comment"> Comment 2... </div>&nbsp;&nbsp; &nbsp; ...&nbsp;</div>那么像这样的greasemonkey脚本就可以修改它:// Wait 2 seconds for the jQuery/AJAX to finish and then modify the HTML DOMwindow.setTimeout(updateHTML, 2000);function updateHTML(){&nbsp; &nbsp; var comments = document.getElementsByClassName("comment");&nbsp; &nbsp; for (i = 0; i < comments.length; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; comments[i].innerHTML = "Modified comment " + i;&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答