2012-02-10 56 views
1

我在Drupal论坛发布了一个类似的问题,但是我没有太多的运气。 我正在将站点从D6升级到D7。到目前为止,它进展顺利,但我得到了一个Javascript错误,我无法为其解决方案。 这是整个剧本的削减版本:升级到Drupal 7后出现Javascript错误

(function($) { 
function sign(secret, message) { 
var messageBytes = str2binb(message); 
var secretBytes = str2binb(secret); 
if (secretBytes.length > 16) { 
secretBytes = core_sha256(secretBytes, secret.length * chrsz); 
} 
var ipad = Array(16), opad = Array(16); 
for (var i = 0; i < 16; i++) { 
ipad[i] = secretBytes[i]^0x36363636; 
opad[i] = secretBytes[i]^0x5C5C5C5C; 
} 
var imsg = ipad.concat(messageBytes); 
var ihash = core_sha256(imsg, 512 + message.length * chrsz); 
var omsg = opad.concat(ihash); 
var ohash = core_sha256(omsg, 512 + 256); 
var b64hash = binb2b64(ohash); 
var urlhash = encodeURIComponent(b64hash); 
return urlhash; 
} 

function addZero(n) { 
return (n < 0 || n > 9 ? "" : "0") + n; 
} 

Date.prototype.toISODate = 
new Function("with (this)\nreturn " + 
"getFullYear()+'-'+addZero(getMonth()+1)+'-'" + 
"+addZero(getDate())+'T'+addZero(getHours())+':'" + 
"+addZero(getMinutes())+':'+addZero(getSeconds())+'.000Z'"); 

function getNowTimeStamp() { 
var time = new Date(); 
var gmtTime = new Date(time.getTime() + (time.getTimezoneOffset() * 60000)); 
return gmtTime.toISODate() ; 
} 
}(jQuery)); 

是不断抛出我看到萤火虫的错误的部分是:

Date.prototype.toISODate = 
new Function("with (this)\n return " + 
"getFullYear()+'-'+addZero(getMonth()+1)+'-'" + 
"+addZero(getDate())+'T'+addZero(getHours())+':'" + 
"+addZero(getMinutes())+':'+addZero(getSeconds())+'.000Z'"); 

萤火虫一直停在“addZero不定义”。 JS从来就不是我的强项,我知道D7已经做了一些改变。我已经将整个脚本包装在“(function($){}(jQuery));”中,但我必须缺少其他的东西。相同的脚本在D6站点上完美工作。

以下是添加了@Pointy建议的整个代码的“固定”版本。我所遗漏的是制作散列到亚马逊的脚本的一部分,以及我声明的一些变量。

(function($) { 
var typedText; 
var strSearch = /asin:/; 
var srchASIN; 
    $(document).ready(function() { 
     $("#edit-field-game-title-und-0-asin").change(function() { 
     typedText = $("#edit-field-game-title-und-0-asin").val(); 
     $.ajax({ 
      type: 'POST', 
      data: {typedText: typedText}, 
      dataType: 'text', 
      url: '/asin/autocomplete/', 
      success:function(){ 
       document.getElementById('asin-lookup').style.display='none'; 
       x = typedText.search(strSearch); 
       y = (x+5); 
       srchASIN = typedText.substr(y,10) 
       amazonSearch(); 
      } 
     }); 
     }); 

     $("#search_asin").click(function() { 
     $("#edit-field-game-title-und-0-asin").val(''); 
     document.getElementById('name-lookup').style.display='none'; 
     $("#edit-field-game-title-und-0-asin").val(''); 
     $("#edit-title").val(''); 
     $("#edit-field-subtitle-und-0-value").val(''); 
     $("#edit-field-game-edition-und-0-value").val(''); 
     $("#edit-field-release-date-und-0-value-date").val(''); 
     $("#edit-field-asin-und-0-asin").val(''); 
     $("#edit-field-ean-und-0-value").val(''); 
     $("#edit-field-amazon-results-und-0-value").val(''); 
     $("#edit-body").val(''); 
     srchASIN = $("#field-asin-enter").val(); 
     amazonSearch(); 
     }); 

     $("#clear_search").click(function() { 
     $("#field-asin-enter").val(''); 
     $("#edit-field-game-title-und-0-asin").val(''); 
     $("#edit-title").val(''); 
     $("#edit-field-subtitle-und-0-value").val(''); 
     $("#edit-field-game-edition-und-0-value").val(''); 
     $("#edit-field-release-date-und-0-value-date").val(''); 
     $("#edit-field-release-dt2-und-0-value-date").val(''); 
     $("#edit-field-asin-und-0-asin").val(''); 
     $("#edit-field-ean-und-0-value").val(''); 
     $("#edit-field-amazon-results-und-0-value").val(''); 
     $("#field-amazon-platform").val(''); 
     $("#field-amazon-esrb").val(''); 
     $("#edit-body-und-0-value").val(''); 
     document.getElementById('asin-lookup').style.display=''; 
     document.getElementById('name-lookup').style.display=''; 
     }); 

     function amazonSearch(){ 
     var ASIN = srchASIN; 
     var azScr = cel("script"); 
     azScr.setAttribute("type", "text/javascript"); 
     var requestUrl = invokeRequest(ASIN); 
     azScr.setAttribute("src", requestUrl); 
     document.getElementsByTagName("head").item(0).appendChild(azScr); 
     } 
    }); 

var amzJSONCallback = function(tmpData){ 
    if(tmpData.Item){ 
     var tmpItem = tmpData.Item; 
    } 
    $("#edit-title").val(tmpItem.title); 
    $("#edit-field-game-edition-und-0-value").val(tmpItem.edition); 
    $("#edit-field-release-date-und-0-value-date").val(tmpItem.relesdate); 
    $("#edit-field-release-dt2-und-0-value-date").val(tmpItem.relesdate); 
    $("#edit-field-asin-und-0-asin").val(tmpItem.asin); 
    $("#edit-field-ean-und-0-value").val(tmpItem.ean); 
    $("#field-amazon-platform").val(tmpItem.platform); 
    $("#field-amazon-publisher").val(tmpItem.publisher); 
    $("#field-amazon-esrb").val(tmpItem.esrb); 
}; 
function ctn(x){ return document.createTextNode(x); } 
function cel(x){ return document.createElement(x); } 
function addEvent(obj,type,fn){ 
    if (obj.addEventListener){obj.addEventListener(type,fn,false);} 
    else if (obj.attachEvent){obj["e"+type+fn]=fn; obj.attachEvent("on"+type,function(){obj["e"+type+fn]();});} 
} 

var styleXSL = "http://www.tlthost.net/sites/vglAmazonAsin.xsl"; 
function invokeRequest(ASIN) { 
    cleanASIN = ASIN.replace(/[-' ']/g,''); 
    var unsignedUrl = "http://xml-us.amznxslt.com/onca/xml?Service=AWSECommerceService&AssociateTag=theliterarytimes&IdType=ASIN&ItemId="+cleanASIN+"&Operation=ItemLookup&ResponseGroup=Medium,ItemAttributes,OfferFull&Style="+styleXSL+"&ContentType=text/javascript&CallBack=amzJSONCallback"; 
    var lines = unsignedUrl.split("\n"); 
    unsignedUrl = ""; 
    for (var i in lines) { unsignedUrl += lines[i]; } 
    // find host and query portions 
    var urlregex = new RegExp("^http:\\/\\/(.*)\\/onca\\/xml\\?(.*)$"); 
    var matches = urlregex.exec(unsignedUrl); 
    var host = matches[1].toLowerCase(); 
    var query = matches[2]; 
    // split the query into its constituent parts 
    var pairs = query.split("&"); 
    // remove signature if already there 
    // remove access key id if already present 
    // and replace with the one user provided above 
    // add timestamp if not already present 
    pairs = cleanupRequest(pairs); 
    // encode the name and value in each pair 
    pairs = encodeNameValuePairs(pairs); 
    // sort them and put them back together to get the canonical query string 
    pairs.sort(); 
    var canonicalQuery = pairs.join("&"); 
    var stringToSign = "GET\n" + host + "\n/onca/xml\n" + canonicalQuery; 
    // calculate the signature 
    //var secret = getSecretAccessKey(); 
    var signature = sign(secret, stringToSign); 
    // assemble the signed url 
    var signedUrl = "http://" + host + "/onca/xml?" + canonicalQuery + "&Signature=" + signature; 
    //document.write ("<html><body><pre>REQUEST: "+signedUrl+"</pre></body></html>"); 
    return signedUrl; 
} 

function encodeNameValuePairs(pairs) { 
    for (var i = 0; i < pairs.length; i++) { 
    var name = ""; 
    var value = ""; 
    var pair = pairs[i]; 
    var index = pair.indexOf("="); 
    // take care of special cases like "&foo&", "&foo=&" and "&=foo&" 
    if (index == -1) { 
    name = pair; 
    } else if (index == 0) { 
    value = pair; 
    } else { 
    name = pair.substring(0, index); 
    if (index < pair.length - 1) { 
     value = pair.substring(index + 1); 
    } 
    } 
    // decode and encode to make sure we undo any incorrect encoding 
    name = encodeURIComponent(decodeURIComponent(name)); 
    value = value.replace(/\+/g, "%20"); 
    value = encodeURIComponent(decodeURIComponent(value)); 
    pairs[i] = name + "=" + value; 
    } 
    return pairs; 
} 

function cleanupRequest(pairs) { 
    var haveTimestamp = false; 
    var haveAwsId = false; 
    var nPairs = pairs.length; 
    var i = 0; 
    while (i < nPairs) { 
    var p = pairs[i]; 
    if (p.search(/^Timestamp=/) != -1) { 
    haveTimestamp = true; 
    } else if (p.search(/^(AWSAccessKeyId|SubscriptionId)=/) != -1) { 
    pairs.splice(i, 1, "AWSAccessKeyId=" + accessKeyId); 
     haveAwsId = true; 
    } else if (p.search(/^Signature=/) != -1) { 
    pairs.splice(i, 1); 
    i--; 
    nPairs--; 
    } 
    i++; 
    } 
    if (!haveTimestamp) { 
    pairs.push("Timestamp=" + getNowTimeStamp()); 
    } 
    if (!haveAwsId) { 
     pairs.push("AWSAccessKeyId=" + accessKeyId); 
    } 
    return pairs; 
} 

function sign(secret, message) { 
    var messageBytes = str2binb(message); 
    var secretBytes = str2binb(secret); 
    if (secretBytes.length > 16) { 
    secretBytes = core_sha256(secretBytes, secret.length * chrsz); 
    } 
    var ipad = Array(16), opad = Array(16); 
    for (var i = 0; i < 16; i++) { 
    ipad[i] = secretBytes[i]^0x36363636; 
    opad[i] = secretBytes[i]^0x5C5C5C5C; 
    } 
    var imsg = ipad.concat(messageBytes); 
    var ihash = core_sha256(imsg, 512 + message.length * chrsz); 
    var omsg = opad.concat(ihash); 
    var ohash = core_sha256(omsg, 512 + 256); 
    var b64hash = binb2b64(ohash); 
    var urlhash = encodeURIComponent(b64hash); 
    return urlhash; 
} 

Date.prototype.toISODate = function() { 
    function addZero(n) { 
    return (n < 0 || n > 9 ? "" : "0") + n; 
    } 
    var d = this; 
    return d.getFullYear() + '-' + 
    addZero(d.getMonth() + 1) + '-' + 
    addZero(d.getDate()) + 'T' + 
    addZero(d.getHours()) + ':' + 
    addZero(d.getMinutes()) + ':' + 
    addZero(d.getSeconds()) + '.000Z'; 
}; 

    function getNowTimeStamp() { 
     var time = new Date(); 
     var gmtTime = new Date(time.getTime() + (time.getTimezoneOffset() * 60000)); 
     return gmtTime.toISODate() ; 
    } 
}(jQuery)); 
+0

控制台中的错误说了什么? – 2012-02-10 13:49:38

+0

我试图通过去除串联来简化函数 - 连接内部的串联比Inception更能伤害我的大脑。 http://jsfiddle.net/ExvRS/我得到一个关于内部函数的undef,也许检查那些被定义和导入的,并且在他们被调用的时候也准备好了。 – 2012-02-10 13:52:51

+0

这很可怕。为什么这样写? – Pointy 2012-02-10 13:52:55

回答

0

这里有一个更好的版本代码的:

Date.prototype.toISODate = function() { 
    function addZero(n) { 
    return (n < 0 || n > 9 ? "" : "0") + n; 
    } 
    var d = this; 
    return d.getFullYear() + '-' + 
    addZero(d.getMonth() + 1) + '-' + 
    addZero(d.getDate()) + 'T' + 
    addZero(d.getHours()) + ':' + 
    addZero(d.getMinutes()) + ':' + 
    addZero(d.getSeconds()) + '.000Z'; 
}; 

那移动“ADDDATE”扩展功能里面,它避免了可怕的with声明。

+0

谢谢!这实际上让事情发生。就像我说的,JS不是我的强项。 – TomP 2012-02-10 14:58:39

+0

该脚本现在正在一路走到最后,但它仍然会引发错误。它从亚马逊获得返回的结果,但现在错误显示“amzJSONCallback未定义”。我实际上可以看到返回的结果。 我会发布整个脚本,但它是390行。 – TomP 2012-02-10 15:10:05

+0

我刚刚添加了脚本的固定版本。我发誓我遇到了“amzJSONCallback未定义”错误,但我不记得我是如何修复它的。 – TomP 2012-02-10 15:41:20

相关问题