Javascript 与单独的Chrome选项卡通信

Javascript 与单独的Chrome选项卡通信,javascript,php,jquery,Javascript,Php,Jquery,我了解此操作的安全风险,以及为什么会出现这样的错误: Uncaught DOMException: Blocked a frame with origin "http://myurl.com" from accessing a cross-origin frame. 所以我想知道是否有一个安全的方法让我这样做 我们公司有两个网站内部的——我的,还有一个不在同一个域上 在我的页面中,我感兴趣的是以一种方式加载第二个页面,这种方式允许我从该页面访问ID元素,拉入这些ID元素包含的数据,并将该数据

我了解此操作的安全风险,以及为什么会出现这样的错误:

Uncaught DOMException: Blocked a frame with origin 
"http://myurl.com" from accessing a cross-origin frame.
所以我想知道是否有一个安全的方法让我这样做

我们公司有两个网站内部的——我的,还有一个不在同一个域上

在我的页面中,我感兴趣的是以一种方式加载第二个页面,这种方式允许我从该页面访问ID元素,拉入这些ID元素包含的数据,并将该数据返回到我的页面中,以便我可以将其显示给我的用户。没有API可供我从第二个源获取此数据。最终,如果有一种方法可以让我将数据输入到源页面中,我会很高兴,但一般来说,注射和攻击的风险太大,我怀疑我是否有办法做到这一点,即使我的意图不是恶意的

我试过几件事:

/* Literally load the page within my own and pull data once it's loaded */
$('#test').load('url.com/site2');

/* load the second page as a variable, then try to access an id on 
that page through the variable */
var win = window.open('url.com/site2');
var test = win.getElementByID('#id_element_i_want_to_pull');

/* I can do something using PHP, but this just loads the page, but doesn't allow 
me to access any of the ID elements on that page which 
doesn't really help me: */
$temp = file_get_contents('url.com/site2');

有什么办法吗?我没有访问第二台服务器上的代码的权限,因此(可能)我没有办法将任何代码放在那里,如果需要的话,我可以访问这些代码。如果这是唯一的方法,我至少想知道它,并知道如果可能的话,这种类型的请求将如何完成。

我认为你在服务器上加载它是正确的,你只需要将它解析成一些你可以使用id来获取东西的东西。我已经有一段时间没有在PHP中做过很多工作了,但是您应该能够使用类来完成这项工作。基本上,你加载文本,把它放到其中一个家伙身上,然后根据他们的id获取元素。

我认为你在服务器上加载它是正确的,你只需要将它解析成可以用来根据id获取内容的东西。我在PHP上做了很多工作,已经有一段时间了,但是您应该能够使用类来完成这项工作。基本上,您加载文本,将其放入其中一个家伙中,然后根据他们的id获取元素。

如果这是一个您无法直接访问的站点,听起来您可以使用DOMDocument类进行一些DOM“胡闹”或“刮除”,如前所述

使用DOMDocument,您可以获取整个页面的内容,然后根据要查找的标记/属性对其进行过滤。我曾经在PHP7中写过类似的内容,这可能会有所帮助:

 class HooverDom {
      public $content;

      public static function checkContentUrl($url) {
         if (stripos($url, 'http') !== 0) {
           return 'http://' . $url;
         }
         return $url;
      }

      public function getContent($url) {
        if (!$this->content) {
           $url = self::checkContentUrl($url);

           if ($url) {
              $this->content = new \DOMDocument( '1.0', 'utf-8' );
              $this->content->preserveWhiteSpace = false;
              // suppress warnings from invalid code
              @$this->content->loadHTMLFile($url);
           }
        }
        return $this->content;
     }

     /**
      * @param $url
      * @param $tag
      *
      * @return array
      * Extract tags that are of interest
      */
     public function getTags($url, $tag) {
        $count = 0;
        $result = array();
        $url = self::checkContentUrl($url);
        if (!$url) return false;

        $elements = $this->getContent($url)->getElementsByTagName($tag);

        foreach ($elements as $node) {
           $result[$count]['value'] = trim(preg_replace('/\s+/', ' ', $node->nodeValue));
           if ($node->hasAttributes()) {
               foreach ($node->attributes as $name => $attr) {
                  $result[$count]['attributes'][$name] = $attr->value;
               }
           }
           $count++;
        }
        return $result;
     }

      /**
       * @param $url
       * @param $attr
       * @param null [$domain]
       *
       * @return array
       * Extract specific attributes rather than tags. Get all tags with *
       * and get their attributes. Optional $domain value keeps all results
       * within supplied domain name
       */
       public function getAttributes($url, $attr, $domain = null) {
         $result = array();
         $elements = $this->getContent($url)->getElementsByTagName('*');

         foreach ($elements as $node) {
            if ($node->hasAttribute($attr)) {
               $value = $node->getAttribute($attr);
               if ($domain) {
                  if (stripos($value, $domain) !== FALSE) {
                     $result[] = trim($value);
                  }
               } else {
                 $result[] = trim($value);
               }
            }
         }
         return $result;
       }
 }

 define('DEFAULT_URL', 'https://developer.mozilla.org/en-US');
 define('DEFAULT_TAG', 'div');

 $vac = new HooverDom();

 $url = strip_tags($_GET['url'] ?? DEFAULT_URL);
 $tag = strip_tags($_GET['tag'] ?? DEFAULT_TAG);

 echo 'Dump of tags: ' . PHP_EOL;
 var_dump($vac->getTags($url, $tag));
这将抓取页面上的所有链接,并为您列出一个列表。这样,您就可以使用某种结构,而不是使用文件\u get\u contents()中的大量字符串

例如,输出如下所示:

 array (size=56)
   0 => 
     array (size=2)
       'value' => string 'Mozilla is working on a new program for developers and other web builders like you. Help shape that program by taking our 10 minute survey: https://googl/forms/Ync2VuTWwAkQFvJx2' (length=178)
  'attributes' => 
    array (size=1)
      'class' => string 'global-notice' (length=13)
   1 => 
     array (size=2)
  'value' => string 'Mozilla is working on a new program for developers and other web builders like you. Help shape that program by taking our 10 minute survey: ' (length=178)
  'attributes' => 
    array (size=1)
      'class' => string 'wrap center' (length=11)

对一些格式错误感到抱歉,如果您需要澄清,请告诉我。您可以循环查看结果,隔离特定的元素ID/类/正在查找的任何其他属性,并获取“值”中的内容


请注意,只有在运行5的情况下,PHP7中才使用NULL coalesce运算符(??)。

如果您无法直接访问该站点,则听起来您可以使用前面提到的DOMDocument类执行一些DOM“胡闹”或“刮除”

使用DOMDocument,您可以获取整个页面的内容,然后根据要查找的标记/属性对其进行过滤。我曾经在PHP7中写过类似的内容,这可能会有所帮助:

 class HooverDom {
      public $content;

      public static function checkContentUrl($url) {
         if (stripos($url, 'http') !== 0) {
           return 'http://' . $url;
         }
         return $url;
      }

      public function getContent($url) {
        if (!$this->content) {
           $url = self::checkContentUrl($url);

           if ($url) {
              $this->content = new \DOMDocument( '1.0', 'utf-8' );
              $this->content->preserveWhiteSpace = false;
              // suppress warnings from invalid code
              @$this->content->loadHTMLFile($url);
           }
        }
        return $this->content;
     }

     /**
      * @param $url
      * @param $tag
      *
      * @return array
      * Extract tags that are of interest
      */
     public function getTags($url, $tag) {
        $count = 0;
        $result = array();
        $url = self::checkContentUrl($url);
        if (!$url) return false;

        $elements = $this->getContent($url)->getElementsByTagName($tag);

        foreach ($elements as $node) {
           $result[$count]['value'] = trim(preg_replace('/\s+/', ' ', $node->nodeValue));
           if ($node->hasAttributes()) {
               foreach ($node->attributes as $name => $attr) {
                  $result[$count]['attributes'][$name] = $attr->value;
               }
           }
           $count++;
        }
        return $result;
     }

      /**
       * @param $url
       * @param $attr
       * @param null [$domain]
       *
       * @return array
       * Extract specific attributes rather than tags. Get all tags with *
       * and get their attributes. Optional $domain value keeps all results
       * within supplied domain name
       */
       public function getAttributes($url, $attr, $domain = null) {
         $result = array();
         $elements = $this->getContent($url)->getElementsByTagName('*');

         foreach ($elements as $node) {
            if ($node->hasAttribute($attr)) {
               $value = $node->getAttribute($attr);
               if ($domain) {
                  if (stripos($value, $domain) !== FALSE) {
                     $result[] = trim($value);
                  }
               } else {
                 $result[] = trim($value);
               }
            }
         }
         return $result;
       }
 }

 define('DEFAULT_URL', 'https://developer.mozilla.org/en-US');
 define('DEFAULT_TAG', 'div');

 $vac = new HooverDom();

 $url = strip_tags($_GET['url'] ?? DEFAULT_URL);
 $tag = strip_tags($_GET['tag'] ?? DEFAULT_TAG);

 echo 'Dump of tags: ' . PHP_EOL;
 var_dump($vac->getTags($url, $tag));
这将抓取页面上的所有链接,并为您列出一个列表。这样,您就可以使用某种结构,而不是使用文件\u get\u contents()中的大量字符串

例如,输出如下所示:

 array (size=56)
   0 => 
     array (size=2)
       'value' => string 'Mozilla is working on a new program for developers and other web builders like you. Help shape that program by taking our 10 minute survey: https://googl/forms/Ync2VuTWwAkQFvJx2' (length=178)
  'attributes' => 
    array (size=1)
      'class' => string 'global-notice' (length=13)
   1 => 
     array (size=2)
  'value' => string 'Mozilla is working on a new program for developers and other web builders like you. Help shape that program by taking our 10 minute survey: ' (length=178)
  'attributes' => 
    array (size=1)
      'class' => string 'wrap center' (length=11)

对一些格式错误感到抱歉,如果您需要澄清,请告诉我。您可以循环查看结果,隔离特定的元素ID/类/正在查找的任何其他属性,并获取“值”中的内容

请注意NULL coalesce操作符(??),它仅在PHP7中用于运行5的情况