Skip to content

Instantly share code, notes, and snippets.

@liling
Created December 5, 2009 04:33
Show Gist options
  • Select an option

  • Save liling/249539 to your computer and use it in GitHub Desktop.

Select an option

Save liling/249539 to your computer and use it in GitHub Desktop.
开心网超级大亨:记录各种产品的价格并显示。
// ==UserScript==
// @name 开心网超级大亨
// @namespace kaixin001.app.rich
// @description 记录开心网超级大亨各种产品的价格
// @include http://www.kaixin001.com/!rich/*
// ==/UserScript==
function parse_number(num) {
var unit = 1;
if (num.match(/万元/)) {
unit = 10000.0;
} else if (num.match(/亿元/)) {
unit = 100000000.0;
}
return parseFloat(num.replace(/[万亿]?元/g, '')) * unit;
}
function sort_price(a , b) {
return parse_number(a) - parse_number(b);
}
function save_history(name, price) {
var history, i;
var key = 'price.' + name;
var saved = GM_getValue(key, null);
if (saved != null) {
history = saved.split(':');
var found = false;
for (i = 0; i < history.length; i++) {
if (history[i] == price) {
found = true;
break;
}
}
if (!found) {
history.push(price);
}
history.sort(sort_price);
saved = history.join(':');
} else {
saved = price;
history = [price];
}
GM_setValue(key, saved);
return history;
}
function show_history_price(product) {
var nes = product.getElementsByClassName('mt5');
var name = nes[0].textContent;
var pes = product.getElementsByClassName('mb10');
var price = pes[0].textContent;
var history = save_history(name, price);
//GM_log(name + ' - ' + history);
// 价格范围
var range = history[0];
if (history.length > 1) {
range += '-' + history[history.length - 1];
}
range = range.replace(/[万亿]?元/g, '');
// 根据当前价格和最高价计算利润率,超过5%的用绿色,超过10%的用蓝色,
// 超过20%的用红色
var color = 'black';
price = parseFloat(price);
var highprice = parseFloat(history[history.length - 1].replace(/[万亿]?元/g, ''));
var profit = (highprice - price) * 100 / price;
if (profit > 20) {
color = 'red';
} else if (profit > 10) {
color = 'blue';
} else if (profit > 5) {
color = 'green';
}
range = ' <span style="float: none; margin: 0; color: ' + color + '">(' + range + ')</span>';
nes[0].innerHTML += range;
var title = ['历史价格:', history.join(','),
';涨价空间:', Math.round((highprice - price) * 100) / 100,
'(', Math.round(profit), '%)'];
product.setAttribute('title', title.join(''));
}
function check_my_asset(product) {
var t, lis;
var name, profit, buyprice;
lis = product.getElementsByTagName('li');
t = lis[1].getElementsByTagName('b');
name = t[0].textContent;
t = lis[4].getElementsByTagName('em');
profit = parse_number(t[0].textContent);
t = lis[2].getElementsByTagName('em');
buyprice = parse_number(t[0].textContent);
//GM_log(name + '收益和买入价:' + profit + ' ' + buyprice);
if ((profit * 100.0 / buyprice) > maxprofit) {
maxprofit = profit * 100.0 / buyprice;
}
//GM_log("当前最大收益:" + maxprofit);
}
function play_sound() {
document.getElementById('head_msgsound_div').innerHTML = '<embed width="1" height="1" flashvars="autoplay=0" menu="false" wmode="opaque" allowscriptaccess="always" quality="high" bgcolor="#ffffff" name="newmsg_sound_swf" id="newmsg_sound_swf" src="http://img1.kaixin001.com.cn/i2/newmsg_sound.1.0.swf" type="application/x-shockwave-flash"/>';
}
var curpage = document.getElementById('nav').getElementsByClassName('curr');
if (curpage[0].textContent == '购买资产') {
// 查找商品价格和名称,并显示出来
var products = document.getElementById('r2_2a').getElementsByTagName('ul');
Array.filter(products, show_history_price);
// 修改页面标题
var nav_bg = document.getElementsByClassName('nav_bg');
var tabelem = nav_bg[0].getElementsByClassName('curr');
document.title = [tabelem[0].textContent, document.title].join(' - ');
}
else if (curpage[0].textContent == '我的资产') {
// 查看商品的收益,到达一定程度则修改页面标题并播放声音
var t = document.getElementsByClassName('list_zc');
var products = t[0].getElementsByTagName('ul');
var maxprofit = 0;
Array.filter(products, check_my_asset);
// 修改页面标题
if (maxprofit >= 25.0) {
document.title = ['赶快卖!', document.title].join(' - ');
play_sound();
} else if (maxprofit >= 10.0) {
document.title = ['涨价啦!', document.title].join(' - ');
} else {
document.title = [curpage[0].textContent, document.title].join(' - ');
}
// 页面三百秒重新载入一次
setTimeout('document.location.reload()', 300000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment