2017-07-21 6 views
-1

Amazon isbns/asinsを入力してハイパーリンクに変換する簡単なコードがあります。これらのハイパーリンクは、上記のisbn/asinのAmazon.com検索です。javascriptでURLを変更する

例のPIC:代わりにハイパーリンクのhttp://imgur.com/a/rYgYt

は、私が製品に直接移動するためのリンクがページを提供したいと考えている検索。

次のように所望のリンクは次のようになります

https://www.amazon.com/gp/offer-listing/ASIN/ref=dp_olp_used?ie=UTF8&condition=used

「ASINを」ASIN/ISBNリンクを生成するために移入する必要がある場合、例えば、次のようになります。

Imが求め誰かが既存のコードを変更して変更を作成する手助けをすることができれば。私のスキルには変更を実装する能力がありません。次のように既存のコードは次のとおりです。

<html> 
<head> 

</head> 
<div><b>ISBN Hyperlinker</b></div> <textarea id=numbers placeholder="paste isbn numbers as csv here" style="width:100%" rows="8" > 
</textarea> <div><b>Hyperlinked text:</b></div> <div id="output" style="white-space: pre"></div> 
<input type="button" id="button" Value="Open All"/> 


<script> 


var input = document.getElementById('numbers'); 
var button = document.getElementById('button'); 
var output = document.getElementById('output') 
var base = 
    'https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=' 

var urls = [] 

//adding an event listener for change on the input box 
input.addEventListener('input', handler, false); 
button.addEventListener('click', openAllUrls, false); 

//function that runs when the change event is emitted 
function handler() { 
    var items = input.value.split(/\b((?:[a-z0-9A-Z]\s*?){10,13})\b/gm); 
    urls=[]; 
    // Build DOM for output 
    var container = document.createElement('span'); 
    items.map(function (item, index) { 
    if (index % 2) { // it is the part that matches the split regex: 
     var link = document.createElement('a'); 
     link.textContent = item.trim(); 
     link.setAttribute('target', '_blank'); 
     link.setAttribute('href', base + item); 
     container.appendChild(link); 
     urls.push(base + item);//add the url to our array of urls for button click 
    } else { // it is the text next to the matches 
     container.appendChild(document.createTextNode(item)) 
    } 
    }); 
    // Replace output 
    output.innerHTML = ''; 
    output.appendChild(container); 
} 
function openAllUrls(){ 
    for(var i=0; i< urls.length; i++){//loop through urls and open in new windows 
    window.open(urls[i]); 
    } 
} 
handler(); // run on load 

</script> 
</html> 

答えて

0

、出力URLを変更

var basePrefix = 'https://www.amazon.com/gp/offer-listing/'; 
var baseSuffix = '/ref=dp_olp_used?ie=UTF8&condition=used'; 

var base = ".....'; 

を交換し、

base + item 

を交換します

basePrefix + item + baseSuffix 
+0

私はあなたを愛しています!!!!!! – tellmeaboutthetaste

関連する問題