iam正在使用浏览器插件,重新计算从美元/欧元到捷克克朗的价格。它主要起作用,但不是每一个价格。我解析源代码,在类或itemprop中选择带有“price”字样的标签,但不是每个价格都标记为这样。简单地说,我不能设定通用规则来找出什么是价格,什么是数字。
例如,他们有
<div class='some-price-tag'>US 99$</div> //this is not problem
<div class='some-price-tag'>US <span class='extra-price'>99$</span></div> //this is a bit tricky
<div class='extra-price-for-you'><span class='text-price-notification'>BEST</span><span itemprop='price-value'>23$</span></div> //it must be hell even for them
<b>56$</b>
还有更多烦恼:)
所以我的问题是:有没有人知道如何设定一些规则来找到所有价格或完全不同的方式来做到这一点? 这是我的功能如何iam寻找价格(它很可怕,我知道):
(它可能有一些错误,因为我很快将所有变量重命名为英语以便更好地理解,希望它有所帮助)
function findPrices(rate)
{
loop(document);
function loop(node)
{
var nodes=node.childNodes;
for (var i=0; i<nodes.length; i++)
{
if(!nodes[i])
{
continue;
}
if(nodes[i].childNodes.length>0)
{
loop(nodes[i]);
var nameTag=nodes[i].tagName;
var contentTag=nodes[i].innerText;
if (contentTag==undefined)
{
contentTag="";
}
var contantProp=nodes[i].getAttribute("itemprop");
if (contentProp==null)
{
contentProp="";
}
var testProp=obsahProp.indexOf("price");
var testClass=nodes[i].className.toString().indexOf("price");
var nextClass=nodes[i].className.toString().indexOf("notranslate");
var testOff=contentTag.toString().indexOf("%");
var testChild=nodes[i].children.length;
var testCalculated=nodes[i].getAttribute("data-calculated");
if (((nameTag=="B")||(nameTag=="P")||(nameTag=="DIV")||(nameTag=="DEL")||(nameTag=="SPAN"))&&((testClass!=-1)||(nextClass!=-1)||(testProp!=-1))&&(testOff==-1)&&(testChild==0)&&(testCalculated!="calculated"))
{
var testNumber=contantTag.match(/[0123456789.,\-]/g);
if (testNumber==null)
{
testNumber="";
}
if (testNumber.length>0)
{
nodes[i].setAttribute("title", "Original: "+contentTag);
nodes[i].setAttribute("data-calculated", "calculated");
nodes[i].innerHTML=escapeHTML(recalculate(contentTag,rate)); //this is price, so its sent to another function
}
}
var testCurrency=contentTag.match(/[$€£]/g);
var testNumbers=contentTag.match(/[0123456789]/g);
if (testCurrency==null)
{
testCurrency="";
}
if (testNumbers==null)
{
testNumbers="";
}
if ((testCurrency.length>0)&&(testNumbers.length>0))
{
nodes[i].setAttribute("title", "Original: "+contentTag);
nodes[i].setAttribute("data-calculated", "calculated");
nodes[i].innerHTML=escapeHTML(recalculate(contentTag,rate)); //this is price too, so its sent to another function
}
}
}
}
}