Amazonのセールで電子書籍を購入しまくってるけどカートがないので合計金額が分からないから作った
Script Editorで作ったからインデントおかしいけど気にしない
※追記:ニコカド祭り2017でまた電子書籍を漁ってる。
久しぶりに使ったら使えなくなってた。
ので、「商品数と合計金額をダイアログに表示する」だけ動くよう修正。たぶん動く。動いた。動けばいいや。
CSVに書き出すほうは使えません。たぶん。
商品数と合計金額をダイアログに表示する
show_browsing_amazon_item_amount.js
GET_PRICE = "document.getElementsByClassName('a-color-price')[0].innerText"
// Chromeから商品情報取得
GET_PRICE = "document.getElementsByClassName('a-color-price')[0].innerText"
// Chromeから商品情報取得
Chrome = Application("Chrome")
amount = 0, counter = 0
Chrome.windows().forEach(function(win){
win.tabs().forEach(function(tab){
if (/http.*amazon.*\/(dp|gp)\/.*/.test(tab.url())){
var price = Chrome.execute(tab, {javascript: GET_PRICE})
if (price){
price = price.match(/[\d,]+/)[0].replace(/\D/g, "");
amount += parseInt(price)
counter++
}
}
})
})
// 表示
Number.prototype.insertComma = function(){return this.toString().replace(/(\d)(?=(\d{3})+$)/g , "$1,")}
app = Application.currentApplication()
app.includeStandardAdditions = true
app.displayDialog("計 " + counter + " 冊 (" + amount.insertComma() + " 円)")
商品タイトルや金額等をCSVに書き出す
generate_browsing_amazon_item_csv.js
Chrome = Application("Chrome")
CSV_NAME = "amazon_items.csv"
CSV_HEADER = "タイトル,価格,紙の本の価格,OFF(%),URL"
GET_TITLE = "document.getElementById('btAsinTitle').innerText"
GET_PRICE = "document.getElementsByClassName('priceLarge')[0].innerText"
GET_PRICE_ORIGINAL = "document.querySelector('.product .listPrice').innerText"
GET_PRICE_SAVING = "document.querySelector('.product .savingsRow .price').innerText"
// Chromeから商品情報を取得
items = [], amount = 0, counter = 0
Chrome.windows().forEach(function(win){
win.tabs().forEach(function(tab){
var url = tab.url()
if (/http.*amazon.*\/dp\//.test(url)){
var title = Chrome.execute(tab, {javascript: GET_TITLE});
var price = Chrome.execute(tab, {javascript: GET_PRICE}).replace(/[\D]/g, '')
var price_org = Chrome.execute(tab, {javascript: GET_PRICE_ORIGINAL})
if (price_org) price_org = price_org.replace(/[\D]/g, '')
var price_off = Chrome.execute(tab, {javascript: GET_PRICE_SAVING})
if (price_off) price_off = price_off.match(/(\d+)%/)[1]
var code = url.match(/\/dp\/[^\/]+/)
items.push([title, price, price_org, price_off, "http://www.amazon.co.jp" + code])
amount += parseInt(price)
counter++
}
})
})
// CSVデータ作成
csv = CSV_HEADER + "\n"
csv += items.map(function(item){
return item.map(function(value){
return '"' + (value || "").replace(/[\r\n]+/g, "").replace(/"/g, '""') + '"'
}).join(",")
}).join("\n")
// ファイルに書き込み
app = Application.currentApplication()
app.includeStandardAdditions = true
path = file_path = Path(app.pathTo("desktop", {from:"user domain"}) + "/" + CSV_NAME)
// ファイルを開く
ref = app.openForAccess(file_path, {writePermission:true})
try {
app.setEof(ref, {to:0}) //全削除
app.write(csv, {to:ref}) //書き込み
} finally {
//ファイルを閉じる
app.closeAccess(ref)
}
Number.prototype.insertComma = function(){return this.toString().replace(/(\d)(?=(\d{3})+$)/g , "$1,")}
app.displayDialog("合計 " + counter + " 冊 (" + amount.insertComma() + " 円)")