Javascript:Schema Explorer Chrome扩展循环需要忽略不相关的嵌套元素

Javascript:Schema Explorer Chrome扩展循环需要忽略不相关的嵌套元素,javascript,google-chrome-extension,schema.org,Javascript,Google Chrome Extension,Schema.org,这对于Javascript专家来说应该是非常容易的 对于那些不知道模式是什么()的人来说,这是搜索引擎阅读网页内容的一种新方式。它的工作原理是用特定的标签标记相关数据 对于那些知道它是什么的人来说,这里有一个chrome扩展()可以很容易地检查页面上的数据。参见示例 现在:扩展存在一个小问题,即by不会跳过/忽略空的嵌套元素。这里有两个例子:第一个可以完美地工作,但是,第二个炸弹,因为标签是空的 第一个示例有效: <div itemscope="" itemtype="http://sch

这对于Javascript专家来说应该是非常容易的

对于那些不知道模式是什么()的人来说,这是搜索引擎阅读网页内容的一种新方式。它的工作原理是用特定的标签标记相关数据

对于那些知道它是什么的人来说,这里有一个chrome扩展()可以很容易地检查页面上的数据。参见示例

现在:扩展存在一个小问题,即by不会跳过/忽略空的嵌套元素。这里有两个例子:第一个可以完美地工作,但是,第二个炸弹,因为标签是空的

第一个示例有效:

<div itemscope="" itemtype="http://schema.org/Movie">
   <h1 itemprop="name">Avatar</h1>
   <div itemprop="director" itemscope="" itemtype="http://schema.org/Person">
      Director: <span itemprop="name">James Cameron</span> (born <span itemprop="birthDate">August 16, 1954</span>)
   </div>
   <span itemprop="genre">Science fiction</span>
   <a href="http://pierreloicdoulcet.fr/movies/avatar-theatrical-trailer.html" itemprop="trailer">Trailer</a>
</div>
<div itemscope="" itemtype="http://schema.org/Movie">
   <div>
     <h1 itemprop="name">Avatar</h1>
     <div itemprop="director" itemscope="" itemtype="http://schema.org/Person">
         Director: <span itemprop="name">James Cameron</span> (born <span itemprop="birthDate">August 16, 1954</span>)
     </div>
     <span itemprop="genre">Science fiction</span>
     <a href="http://pierreloicdoulcet.fr/movies/avatar-theatrical-trailer.html" itemprop="trailer">Trailer</a>
   </div>
</div>

阿凡达
导演:詹姆斯·卡梅隆(生于1954年8月16日)
科幻小说
秒示例给出问题:

<div itemscope="" itemtype="http://schema.org/Movie">
   <h1 itemprop="name">Avatar</h1>
   <div itemprop="director" itemscope="" itemtype="http://schema.org/Person">
      Director: <span itemprop="name">James Cameron</span> (born <span itemprop="birthDate">August 16, 1954</span>)
   </div>
   <span itemprop="genre">Science fiction</span>
   <a href="http://pierreloicdoulcet.fr/movies/avatar-theatrical-trailer.html" itemprop="trailer">Trailer</a>
</div>
<div itemscope="" itemtype="http://schema.org/Movie">
   <div>
     <h1 itemprop="name">Avatar</h1>
     <div itemprop="director" itemscope="" itemtype="http://schema.org/Person">
         Director: <span itemprop="name">James Cameron</span> (born <span itemprop="birthDate">August 16, 1954</span>)
     </div>
     <span itemprop="genre">Science fiction</span>
     <a href="http://pierreloicdoulcet.fr/movies/avatar-theatrical-trailer.html" itemprop="trailer">Trailer</a>
   </div>
</div>

阿凡达
导演:詹姆斯·卡梅隆(生于1954年8月16日)
科幻小说
我看了一下扩展,它实际上与一个javascript文件结合在一起,完成了大部分工作。下面是执行循环的代码,但是它需要能够跳过空的嵌套元素,通常可能更健壮一些

    var __explore = function(node, parentData)
    {
        if (parentData === null || parentData === undefined)
        {
            parentData = __dataTree;
        }
        if (node.getAttribute)
        {
            var isItemScope = node.getAttribute('itemscope');
            var hasItemProp = node.getAttribute('itemprop');
            var itemtype = node.getAttribute('itemtype');


            var childs = node.childNodes;

            var i = 0;

            var tmp = new Array();

            while (i < childs.length)
            {
                if (isItemScope !== null)
                    __explore(childs[i], tmp);
                else
                    __explore(childs[i], null);
                ++i;
            }

            if (isItemScope !== null)
            {
                parentData.push({name : 'scope', value : hasItemProp, type : itemtype, childs : [tmp], node : node});
            }
            else if (hasItemProp && parentData)
            {
                parentData.push({name : hasItemProp, value :  node.innerText});
            }
        }
    }
var\uuuu explore=函数(节点,父数据)
{
if(parentData==null | | parentData==undefined)
{
parentData=\uuuuDataTree;
}
if(node.getAttribute)
{
var isItemScope=node.getAttribute('itemscope');
var hasItemProp=node.getAttribute('itemprop');
var itemtype=node.getAttribute('itemtype');
var childs=node.childNodes;
var i=0;
var tmp=新数组();
而(i
以下是
contentscript.js的完整版本


希望有人能帮我。为了记录在案,我已经联系了作者,但他正忙于更紧急的事情。

我让它如预期的那样工作:但我必须承认,这有点草率。这段代码可能需要进行更多的重构,以使其适用于所有情况并使其可读。

我使其按预期工作:但我必须承认,这有点太草率了。此代码可能需要进行更多的重构,以使其适用于所有情况并使其可读。

谢谢,我将查看它是否在更大的测试床上工作。谢谢,我将查看它是否在更大的测试床上工作。