使用Chrome扩展程序在页面加载前如何隐藏所有内容 [英] How to hide everything before page load with Chrome Extension

查看:179
本文介绍了使用Chrome扩展程序在页面加载前如何隐藏所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过使用内容脚本

manifest



< pre $ content_scripts:[
{
matches:[*:// * / *],
js:[ js / content_script.js]
}
]

content_script.js

  _ini(); 
function _ini(){
document.body.style.display =none;
}

但它首先加载页面,然后隐藏它。



所以我试过 webNavigation

  chrome.webNavigation.onCommitted.addListener(function(details){
alert('webnav');
document.body.style.display =none;
});

但上面的工作也没有。在页面加载之前,页面提示 webnav ,但显示没有任何结果。



我真正需要的是隐藏整个而没有向客户展示任何元素。任何想法?

解决方案

使用Chrome扩展程序在页面加载前隐藏所有内容

<使用 run_at 作为@ParagGangil提及



将它包含在 manifest

$ $ $
$匹配:[*:/ $ / * / *],
js:[content_script.js],
run_at:document_start//< - 这部分是键
}
]

更多关于 run_at:document_start

content_script.js

  _ini(); 

函数_ini(){

document.getElementsByTagName(html)[0] .style.display =none;

document.getElementsByTagName(html)[0] .style 。显示=块; //再次显示它们



$ b $ c


$ b

as @Xan评论 document.body 尚未在加载 content_script.js 时创建我们定位< html> 标记


I tried using content scripts

manifest

"content_scripts": [
    {
        "matches": ["*://*/*"],
        "js": ["js/content_script.js"]
    }
]

content_script.js

_ini();
function _ini(){
    document.body.style.display="none";
}

But it loads the page first and then it hides it.

So I tried webNavigation

chrome.webNavigation.onCommitted.addListener(function(details){
    alert('webnav');
    document.body.style.display="none";
});

But the above didnt work too. The page alerts webnav before page load but display none didnt work.

All I really need is to hide the entire site without showing the client any elements at all. Any ideas?

解决方案

To hide everything before page load with Chrome Extension

Use run_at As @ParagGangil mentioned

Include it in manifest

"content_scripts": [
    {
        "matches": ["*://*/*"],
        "js": ["content_script.js"],
        "run_at": "document_start" //<-This part is the key
    }
]

More on "run_at": "document_start"

And this should be inside content_script.js

_ini();

function _ini(){

    document.getElementsByTagName("html")[0].style.display="none";

    window.onload=function(){

        //do your stuff

        document.getElementsByTagName("html")[0].style.display="block"; //to show it all back again

    }

}

as @Xan commented document.body is not yet constructed during the load of content_script.js so we target the <html> tag

这篇关于使用Chrome扩展程序在页面加载前如何隐藏所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆