我有一個Greasemonkey腳本,用於跟蹤unicreatures.com
上的不同事物。與if語句混淆錯誤
我想要統計的一件事是點擊頁面上的一些鏈接,但不是所有鏈接。
這些都需要計算在內, http://unicreatures.com/explore.php?area=sea&id=89&key=bf12
這些不應該算作, http://unicreatures.com/explore.php?area=sea&gather=5&enc=394844&r=
有人幫我找出一個正則表達式是做了什麼,我想,但我不得不給每個不同的探索位置(area=**
)編碼到它,所以我決定不起作用。
的正則表達式版本
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
var link = links[i];
if (/area=sea(?!\&gather)/.test(link.href)) {
link.addEventListener('click', function() {
localStorage.steps=Number(localStorage.steps)+1
// alert(localStorage.steps + ' in Sargasso');
}, true);
}
}
很顯然,我不想要一個十億如果的area=
不同值的語句,我無法找到一個方法來添加一個變量到正則表達式。
所以,我終於找到了一些字符串操作命令,並放在一起這樣:
var url = window.location.href;
var startOf=url.indexOf("=")+1;
var endOf=url.indexOf("&");
var loc =url.substring(startOf,endOf);
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
var link = links[i];
if (url.indexOf("area=")>=0 && url.indexOf("gather=")<0) {
link.addEventListener('click', function() {
localStorage.steps=Number(localStorage.steps)+1
localStorage[loc+"Steps"]=Number(localStorage[loc+"Steps"])+1
alert(localStorage[loc+"Steps"] +" in local"+loc);
}, true);
}
}
出於某種原因,即使第二個條件是虛假它計數。這是一個簡單的例子,我得到的語法錯誤的地方,或者這是一個Greasemonkey錯誤?我在控制檯中沒有任何錯誤。
不應該這是'url.indexOf(「gather =」)<0'? –
其實你是對的...我會嘗試...但是因爲它應該返回-1,即使它是'== - 1'或'=== - 1'我不知道這是否會有所幫助....測試這個腳本腳本讓我收集了至少20個雞蛋,我不想要到目前爲止大聲笑 –
@BGR Nope仍然計算蛋點擊以及步驟... argggg ,但我改變了這個問題,因爲這是一個很好的觀點。 –