最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I modify a web page using google chrome content script before the DOM is processed? - Stack Overflow

programmeradmin0浏览0评论

Using chrome content scripts I want to remove several iframes in a webpage before their content is loaded.

I found, that using the property run_at:document_start in the extentions manifest my javascript is executed just after the main page request and before the DOM is processed and images, iframes etc. are loaded. Logicaly at this point the DOM structure is not availabel and I can not modify the page using mands like:

myelement=document.getElementById('iframeX');

myelement.parentNode.removeChild(myelement);

How can i access and modify the reqeusted page data then?

Using chrome content scripts I want to remove several iframes in a webpage before their content is loaded.

I found, that using the property run_at:document_start in the extentions manifest my javascript is executed just after the main page request and before the DOM is processed and images, iframes etc. are loaded. Logicaly at this point the DOM structure is not availabel and I can not modify the page using mands like:

myelement=document.getElementById('iframeX');

myelement.parentNode.removeChild(myelement);

How can i access and modify the reqeusted page data then?

Share Improve this question edited Jun 10, 2011 at 21:43 Cornelius asked Jun 10, 2011 at 9:35 CorneliusCornelius 1552 silver badges9 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 7

You need to trigger your content script on document_start and add a beforeload listener - you can use event.preventDefault to selectively block content from loading and then remove the frame that tried to load the content:

    document.addEventListener('beforeload', function(event) {
        if (event.url.match('facebook')) {
            event.preventDefault();
            $(event.target).remove();
        }
    }, false);

I've blogged about an equivalent approach to using the beforeload event for Firefox also.

The manifest.json for this should contain content_scripts like:

"content_scripts": [
    {   
        "matches": ["*://*/*"], 
        "js": [ "scan.js" ],
        "run_at": "document_start",
        "all_frames" : true
    }
]

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论