2
我一直在嘗試爲Google Chrome編寫我的第一個用戶腳本。它會檢查路由器頁面並重新連接,直到獲得有效的IP地址。谷歌瀏覽器 - 未捕獲TypeError:無法調用方法'getElementsByClassName'爲null
我將腳本保存爲IPchecker.user.js,並通過擴展頁面將其安裝在Google Chrome中。但是,當我刷新頁面,JavaScript控制檯拋出錯誤:
Uncaught TypeError: Cannot call method 'getElementsByClassName' of null
從我的理解,這意味着它不知道是什麼文件。但是,這怎麼會發生?我認爲腳本將在文檔準備好後運行?這是不是真的?這是在JavaScript控制檯中工作的代碼。
// ==UserScript==
// @name WAN checker
// @version 0.0.1
// @description Check tomato IP addresses and reconnect as needed
// @match *://192.168.1.1/status-overview.asp
// ==/UserScript==
(function reconnect() {
wan1ip = document.getElementById('wan1ip').getElementsByClassName('content')[0].innerText.split('.').slice(0,2).join('.');
if (wan1ip != "333.3333") {
wan_connect("1");
setTimeout("reconnect()",2000);
}
wan2ip = document.getElementById('wan2ip').getElementsByClassName('content')[0].innerText.split('.').slice(0,2).join('.');
if (wan2ip != "333.333") {
wan_connect("2");
setTimeout("reconnect()",2000);
}
})();
哇!確實。它實際上是'wanip'而不是'wan1ip'。我花了2個小時沒有找到它。謝謝。 – ericcire