Last active
August 29, 2015 14:08
-
-
Save user19/81f3f6b80cdc3680f49c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var main = function(){ | |
if((location.hostname === "s.kakaku.com") || (location.hostname === "m.kakaku.com")) | |
return alert("PC表示でご利用ください。"); | |
if(location.hostname !== "kakaku.com") | |
return alert("ドメインが'kakaku.com'ではありません。"); | |
if(location.pathname.search(/item/) === -1) | |
return alert("商品ページでご利用ください。"); | |
if(set) | |
return setItem(); | |
if(get) | |
return getItem(); | |
if(rm) | |
return rmItem(); | |
} | |
main(); | |
function setItem(){ | |
var select = function(){ | |
for(var i=1;i<=100;i++) | |
if(!(location.pathname+i in window.localStorage)) | |
return location.pathname+i; | |
} | |
var category = "[" + document.getElementsByClassName("path btmPath")[0].getElementsByTagName("a")[2].getElementsByTagName("span")[0].innerHTML + "]"; | |
var title = document.getElementById("titleBox").getElementsByTagName("h2")[0].innerHTML; | |
var price = document.getElementById("minPrice").getElementsByTagName("span")[0].innerHTML; | |
var obj = {category:category,title:title,price:price}; | |
window.localStorage.setItem(select(),JSON.stringify(obj)); | |
getItem(); | |
} | |
function getItem(){ | |
var str = ""; | |
var sum = 0; | |
for(key in window.localStorage){ | |
if(key.search(/item/) >= 0){ | |
var obj2 = JSON.parse(window.localStorage.getItem(key)); | |
for(key2 in obj2){ | |
str += obj2[key2] + " "; | |
if(key2 === "price") | |
sum += Number(obj2[key2].slice(1).replace(",","")); | |
} | |
str += "\n"; | |
} | |
} | |
str += "合計" + " " + "\\" + String(sum).replace(/(\d)(?=(\d\d\d)+(?!\d))/g,'$1,'); | |
alert(str); | |
} | |
function rmItem(){ | |
var flag = true; | |
var res = window.prompt("削除したいアイテムの文字列を指定してください。(一部でも可)\n(すべての場合は'all'を指定)","all"); | |
for(key in window.localStorage){ | |
if(key.search(/item/) >= 0){ | |
if(res.search(/all/i) === 0){ | |
window.localStorage.removeItem(key); | |
}else if(flag){ | |
var obj2 = JSON.parse(window.localStorage.getItem(key)); | |
for(key2 in obj2){ | |
if(obj2[key2].search(res) >= 0){ | |
window.localStorage.removeItem(key); | |
flag = false; | |
} | |
} | |
} | |
} | |
} | |
getItem(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment