周末在家閑來無聊,發現京東有幾十個待評價的商品,覺得JD的羊毛不薅白不薅。廢話不多說看代碼:

看到這樣的結構是否似成相識??這不就是一個典型的web站點的結構?其實差不多啦,最大的區別就是多了一個manifest.json 文件。

前面的配置信息,猜猜大概也就知道是什么了。我們這個JD插件最重要的一塊就是content scripts. 這里面有三個參數最重要
1. Matches: 這個是說這個contentscript只有在域名 https(/http)://club.jd.com 下才會運行。
2. js: 插件需要js文件
3. run at: 插件導入的js文件放在網頁的位置。
來看看content js:
window.addEventListener("load", myMain, false);
function myMain(evt) {
// DO YOUR STUFF HERE.
console.log("JD helper running!");
console.log(window.location.href);
console.log(document.URL);
if (document.URL === "https://club.jd.com/myJdcomments/myJdcomment.action") {
if ($("a[class='btn-def']:contains('評價')") != undefined) {
$("a[class='btn-def']:contains('評價')")[0].click();
}
}
if (document.URL.indexOf("orderVoucher.action") > -1) {
console.log("auto commments star");
console.log($('span[data-id="A1"]'));
$('span[data-id="A1"]').each(function () {
$(this).click();
});
$('span[class="star star5"]').each(function () {
$(this).click();
});
var cnt = 0;
$("a[class='tag-item']").each(function () {
cnt++;
if (cnt % 2 == 1) {
$(this).addClass("tag-checked");
}
});
$('textarea[placeholder="商品是否給力?快分享你的購買心得吧~"]').val("東西挺好的值得購買,下次還會再來。");
$('a[class="btn-submit"]')[0].click();
}
if (document.URL.indexOf("saveCommentSuccess.action") > -1) {
window.location.href = "https://club.jd.com/myJdcomments/myJdcomment.action";
}
}
看到這些代碼是不是覺得很簡單?
這個項目的地址:https://github.com/raycdut/JDHelper
參考書籍:http://www.ituring.com.cn/book/1421
