Firefoxを使用している場合、次の答えが当てはまるので幸運です。Chromeを使用している場合、幸運ははるかに少なくなります。この回答の下部を参照してください。
Greasemonkey は、DOMがロードされるとユーザースクリプトを起動するため、「DOM対応」リスナーを実装する必要はありません。
また、あなたは、Firefox上にあるので、あなたは、いくつかの近代的なお菓子を使用することができますfor...of
、let
。
結果として得られるGreasemonkeyスクリプトは次のとおりです。
// ==UserScript==
// @name Remove Google redirects
// @namespace google
// @description Remove redirects from Google Search result links.
// @include https://www.google.*/*
// @version 1
// @grant none
// ==/UserScript==
for (let element of document.querySelectorAll('#res .r > a')) {
element.removeAttribute('onmousedown');
}
おかげでlet
、したがって、あなたは上記のコードを囲む必要はありません、ローカルの宣言がない生命維持。
残念なChrome(Tampermonkey)ユーザーの場合:
スクリプトが実行された時点ではリンクは見つかりませんがdocument.readyState === 'complete'
、その結果…タイマー付きのループを実装する必要があります。
したがって、次のようになります。
// ==UserScript==
// @name Remove Google redirects
// @namespace google
// @description Remove redirects from Google Search result links.
// @include https://www.google.*/*
// @version 1
// @grant none
// ==/UserScript==
(function removeGoogleRedirects() {
var links = document.querySelectorAll('#res .r > a');
if (links.length === 0) {
setTimeout(removeGoogleRedirects, 100);
return;
}
for (var link of links) {
link.removeAttribute('onmousedown');
}
})();
2018年10月更新:
Googleページのマークアップが変更されたh3.r
ため、に変更する必要がありますdiv.r
。
さらに進んで置き換えh3.r > a
ました#res .r > a
(「tag.class」を「.class」に置き換え、セキュリティとして親を追加して、セレクターがあまり一般的ではないようにしました)。