Chrome 行情抓取插件
上班想偷偷摸摸看行情?自己动手写插件啊,尝试写了一个,新建文件夹,命名为StockMonitor,放入文件如下:
- 3个.png图标文件,19X19、48X48、128X128,命名为19、48、128用作插件图标
- manifest.json:配置文件
{
"update_url": "https://clients2.google.com/service/update2/crx",
"name": "StockMonitor",
"version": "1.0.0",
"description": "StockMonitor",
"manifest_version": 1,
"browser_action": {
"default_icon": "19.png",
"default_title": "StockMonitor",
"default_popup": "popup.html"
},
"icons": {
"16": "19.png",
"48": "48.png",
"128": "128.png"
},
"permissions": [
"http://hq.sinajs.cn/",
"storage"
]
}
- popup.html:插件主界面
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
table {
width: 150px;
font-size: 10px;
text-align: center;
}
table tr th {
font-family: 'Courier New', Courier, monospace;
font-size: 12px;
}
.tr-red {
color: red;
}
.tr-green {
color: green;
}
</style>
</head>
<div id="maincontent">
<table>
<tr>
<th>名称</th>
<th>现价</th>
<th>涨幅</th>
</tr>
<tr id="510300">
<td>300ETF</td>
<td>0.000</td>
<td>-0.00%</td>
</tr>
<tr id="601288">
<td>农业银行</td>
<td>0.000</td>
<td>+0.00%</td>
</tr>
<tr id="000157">
<td>中联重科</td>
<td>0.000</td>
<td>+0.00%</td>
</tr>
</table>
</div>
<div>
<script type="text/javascript" charset="utf-8" src='jquery.min.js'></script>
<script type="text/javascript" charset="utf-8" src='refresh.js'></script>
</div>
</html>
- refresh.js:数据获取脚本
var stock = {
format_number: function(s, n) {
n = n > 0 && n <= 20 ? n : 2;
s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
var l = s.split(".")[0].split("").reverse(),
r = s.split(".")[1];
t = "";
for (i = 0; i < l.length; i++) {
t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
}
return t.split("").reverse().join("") + "." + r;
},
init: function() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
var quoteStr = xmlhttp.responseText;
quoteStr = quoteStr
.replace(/var hq_str_s_sz/g, '')
.replace(/var hq_str_s_sh/g, '')
.replace(/=/g, ',')
.replace(/"/g, '')
.replace(/\n/g, '')
.replace(/\r\n/g, '');
quoteStr = quoteStr.substr(0, quoteStr.length - 1);
var quotes = quoteStr.split(';');
for (var i = 0; i < quotes.length; i++) {
var quote = quotes[i].split(',');
var code = quote[0]; //代码
var price = parseFloat(quote[2]); //价格
var pct = quote[4]; //百分比
console.log(quote[4] + ',' + pct);
var tr = document.getElementById(quote[0]);
tr.children[1].innerText = stock.format_number(price, 3);
if (pct < 0) {
tr.setAttribute('class', 'tr-green');
tr.children[2].innerText = quote[4] + '%';
} else {
tr.setAttribute('class', 'tr-red');
tr.children[2].innerText = '+' + quote[4] + '%';
}
}
}
}
xmlhttp.open("GET", "http://hq.sinajs.cn/rn=1522216317579&list=s_sh510300,s_sh601288,s_sz000157", true);
xmlhttp.send();
}
};
window.onload = function() {
stock.init();
}
在扩展程序里加载已解压的扩展程序即可使用,想打包什么的自行百度~