Get the contents of a class inside an iframe, with Greasemonkey

六眼飞鱼酱① 提交于 2019-12-24 09:58:27

问题


I have a Greasemonkey script which uses jQuery and detects if a "class" is present in the document.

var damageMessage = $(".mw_error").text();

This works fine when the document loads and damageMessage holds a text string.

The problem is that the "class" .mw_error is inside an iframe and when you click a new link inside, the iframe content loads without a page refresh but the contents of "class" do change and I want to get the new contents or text string.

I’m guessing it’s because the document doesn’t reload that the Greasemonkey script doesn’t load again and try and find the updated class contents. Somehow I need to get the new contents of the class.

Any help on this would be much appreciated :)


回答1:


Use the iframe on load method and get the text of the required element like this. This will ensure that every time iframe is refreshed you will get the new text that is loaded inside iframe.

$("iframe").load(function(){

   var damageMessage = $(this).contents().find(".mw_error").text();

});


来源:https://stackoverflow.com/questions/7015441/get-the-contents-of-a-class-inside-an-iframe-with-greasemonkey

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