Javascript 如何阻止frame buster破坏WordPress自定义页面?

Javascript 如何阻止frame buster破坏WordPress自定义页面?,javascript,wordpress,framebusting,Javascript,Wordpress,Framebusting,请看一个简单的framebuster: <script type="text/javascript"> if (top.location != self.location) { top.location = self.location.href; } </script> 如果(top.location!=self.location){ top.location=self.location.href; } 然而,它似乎工作得太

请看一个简单的framebuster:

<script type="text/javascript"> 
    if (top.location != self.location) { 
        top.location = self.location.href; 
    } 
</script>

如果(top.location!=self.location){
top.location=self.location.href;
} 
然而,它似乎工作得太好了。它会打断WordPress中的customizer admin页面,当您自定义某些设置时,网站会在一个框架中预览。我怎样才能修改它,使它打断帧…但不是那个帧

也许你可以:

  • 检查它是否在同一个域上并且不破坏那些帧
  • 使用某种正则表达式匹配检查特定的自定义程序URL

  • 我对这两种解决方案都很满意,但我不知道如何实现它们。任何帮助都将不胜感激。

    因此显然top.location有一个名为pathname的字段,其中包含不带域的URL。通过检查该字段,我可以排除特定的管理员页面

                <script type="text/javascript">
                function parentIsEvil(parent) {
                    var html = null;
                    try {
                      var doc = top.location.pathname;
                    } catch(err){
                      // do nothing
                    }
                    console.log(doc);
                    return(doc != "/wp-admin/customize.php");
                }
                console.log(canAccessParent());
                if (top.location != self.location && parentIsEvil()) { 
                    top.location = self.location.href;
                }
            </script>';
    
    
    函数parentIsEvil(父级){
    var html=null;
    试一试{
    var doc=top.location.pathname;
    }捕捉(错误){
    //无所事事
    }
    控制台日志(doc);
    返回(doc!=“wp admin/customize.php”);
    }
    log(canAccessParent());
    如果(top.location!=self.location&&parentIsEvil()){
    top.location=self.location.href;
    }
    ';
    

    更新:我添加了检查跨域并捕获任何错误的解决方案。

    您可以使用
    wp\u customize
    query arg和
    is\u user\u logged\u in
    函数将您的javascript封装在if语句中,如下所示:

    <?php if ( ! ( isset( $_GET[ 'wp_customize' ] ) && is_user_logged_in() ) ): ?>
    <script type="text/javascript"> 
        if (top.location != self.location) { 
            top.location = self.location.href; 
        } 
    </script>
    <?php endif; ?>
    
    
    如果(top.location!=self.location){
    top.location=self.location.href;
    } 
    
    您是否可以将条件设置为
    (top.location!=self.location&&top.location.href!='http://www.example.com/page.html“)
    …这是安装在多个站点上的插件,因此无法检查特定URL,因为它们都有不同的域名。相反,我需要检查字符串中没有域的特定页面(wp admin/customize.php),我不知道如何做。。。我只是想找一个正则表达式来解决这个问题,但是你打败了我,哈哈。。。加上你的更干净:D@Stretch一旦我发现了“pathname”字段,创建这个解决方案就容易多了。这个解决方案阻止它破坏该页面上的框架,但由于跨域错误问题,它破坏了它的框架破坏能力。