开发者

how to know when the DOM is ready again after changing URL with window.location.href

开发者 https://www.devze.com 2023-04-12 19:04 出处:网络
I\'m writing a Firefox extension using Add-on SDK and I successfully scan开发者_StackOverflow中文版ned a site from a content script and want to go deeper now. However, when I change the url with

I'm writing a Firefox extension using Add-on SDK and I successfully scan开发者_StackOverflow中文版ned a site from a content script and want to go deeper now. However, when I change the url with

window.location.href = "http://www.thatsite.com/foo.php";
alert($('#abc');

I can't get access to the new DOM. The alert appears before the site is fully read. I tried $(document).ready(function() but it seems that this works only once...

How can I wait until the site is fully build?


Your content script only lives as long as the page is loaded - if you navigate to a different page this content script instance will be unloaded. Another instance of the content script will be loaded into the new page however, you can do everything necessary there. The only problem is passing data from the old instance to the new one. You could send your data to the add-on using postMessage() and then have the add-on send it to the new content script in the onAttach() handler. Whether this is a possibility in your case depends on what you are trying to achieve (you didn't say that in the question).


You need to move your JS to a higher level if you want to interact with a tab with changing location. See: MDC docs. As it is, you JS is being applied once within the context of the current page. Once a new page loads, it is effectively non-existent.

Really, you shouldn't be interacting with the page like that from an addon anyways. That's behavior akin to a Greasemonkey userscript, not a Firefox extension.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号