admin管理员组文章数量:1221380
I'm trying to write a Google chrome extension that grabs the element in the page by its class name. The idea is to use the content script to grab these elements and pass the messages to the extensions and pop up. However, my content script always end up executing before the entire page is loaded so I can't grab the elements that I want. I try using window.loaded
and document.loaded
but it didn't work. I also tried wait an interval but the script always ended up executing at the exact same stop point.
// my content script
if (document.readyState == "complete"){
var board_name_arr = [];
var node = document.getElementsByClassName('Board');
for (var i = 0; i < node.length; ++i){
var board_name = node[i].getElementsByClassName('boardName')[0].textContent;
board_name_arr[i] = board_name;
}
if (Object.keys(board_name_arr).length){
alert("found board");
}
}
Is there a way to force it to run after ? Or should I not be using content script approach?
I'm trying to write a Google chrome extension that grabs the element in the page by its class name. The idea is to use the content script to grab these elements and pass the messages to the extensions and pop up. However, my content script always end up executing before the entire page is loaded so I can't grab the elements that I want. I try using window.loaded
and document.loaded
but it didn't work. I also tried wait an interval but the script always ended up executing at the exact same stop point.
// my content script
if (document.readyState == "complete"){
var board_name_arr = [];
var node = document.getElementsByClassName('Board');
for (var i = 0; i < node.length; ++i){
var board_name = node[i].getElementsByClassName('boardName')[0].textContent;
board_name_arr[i] = board_name;
}
if (Object.keys(board_name_arr).length){
alert("found board");
}
}
Is there a way to force it to run after ? Or should I not be using content script approach?
Share Improve this question asked Jan 28, 2015 at 21:28 Irene YehIrene Yeh 951 gold badge1 silver badge3 bronze badges 1- Probably duplicate with this one: stackoverflow.com/questions/13917047/… – Dayton Wang Commented Jan 28, 2015 at 21:42
4 Answers
Reset to default 12Probably the page loads its content dynamically though AJAX, so the elements you want to find may be still not loaded when the document state is ready. Even if some part of content is loaded on start, more content may come later. To solve this issue correctly I'd recommend you use MutationObserver techniques. I used it in my Chrome extension to inject the 'Favorite' button to each Facebook post and it worked perfectly.
See the code sample:
var obs = new MutationObserver(function (mutations, observer) {
for (var i = 0; i < mutations[0].addedNodes.length; i++) {
if (mutations[0].addedNodes[i].nodeType == 1) {
$(mutations[0].addedNodes[i]).find(".userContentWrapper").each(function () {
injectFBMButton($(this));
});
}
}
injectMainButton();
});
obs.observe(document.body, { childList: true, subtree: true, attributes: false, characterData: false });
Without jQuery you can do this defining a callback function on the page load event. You can do it this way :
var loadfunction = window.onload;
window.onload = function(event){
//enter here the action you want to do once loaded
if(loadfunction) loadfunction(event);
}
Or this way :
window.addEventListener("load", function load(event){
window.removeEventListener("load", load, false); //remove listener, no longer needed
//enter here the action you want to do once loaded
},false);
I know, this question was posted way too long. Nevertheless, hoping it would be helpful for anyone scouting like me.
Chrome Extension's ContentScript have an option called run_at with options: document_start , document_idle & document_end. Documentation: https://developer.chrome.com/extensions/content_scripts#run_time
.
.
.
"content_scripts": [
{
"js": ["PATH_TO_YOUR_SCRIPT.JS"],
"run_at": "document_end"
}
]
.
.
.
is exactly what you needed (is the partial content of manifest.json file.
Try to use Jquery
$(document).ready(function(){ //your code here });
http://learn.jquery.com/using-jquery-core/document-ready/
本文标签: javascriptHow to execute content script after the page is loaded completelyStack Overflow
版权声明:本文标题:javascript - How to execute content script after the page is loaded completely - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739300600a2157119.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论