Google Chrome の最初のユーザー スクリプトを作成しようとしています。ルーターのページを確認し、有効な IP アドレスを取得するまで再接続します。
スクリプトを 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);
}
})();