0

私が使用しているウェブサイトは、すばらしい JavaScript で書かれています。どこでもグローバル、クロージャーはほとんどなく、厳密モードを使用します。これにより、独自の機能を Web サイトに挿入することが非常に難しくなっています。

ウェブサイト クライアント オブジェクトはjQuery.ready()呼び出しで初期化されます。

$(window).ready(function () {
    var a, b, c, d;

    // Setup global data [...]
    // Setup configuration [...]

    a = GlobalFoo.ConstructorA();
    b = GlobalFoo.ConstructorB(a);
    // Really wish I could put stuff here
    c = GlobalFoo.ConstructorC(a, b);
    d = GlobalFoo.ConstructorD(b, c);
    // etc.
});

b.someMethod()たとえば、他のコンストラクターが呼び出される前に自分のコードに置き換えるにはどうすればよいですか?

準備完了イベントの発生を停止したり、独自のコードに置き換えたりすることはできますか? それは非常に小さいので、変更したバージョンをコードに複製することができます。

4

1 に答える 1

2

もう少し検索した後、dindog によるこの素晴らしいページを見つけました。GreaseSpot wiki には、 について説明しているこのページもあります@run-at

@run-atユーザースクリプトを他のすべてのコードの前に実行できます。イベントを使用すると、beforescriptexecute実行前に各スクリプトを確認できます。その後、スキップまたは変更できます。

私の最終的な解決策は次のとおりです。

// ==UserScript==
// @name        ...
// @description ...
// @namespace   ...
// @include     ...
// @version     1
// @run-at      document-start
// @grant       none
// ==/UserScript==

(function (w) {
    // Remove the current jQuery ready code
    function pre_script_execute_handler (e) {
        var target = e.target;
        if (target.innerHTML.indexOf("$(window).ready(") != -1) {
            console.log('Removed ready');
            e.preventDefault();
            e.stopPropagation();
            addReady();
        }
    }
    w.addEventListener('beforescriptexecute', pre_script_execute_handler);

    // Add new jQuery ready code
    function addReady () {
        console.log('Adding new ready');
        w.$(window).ready(function () {
            console.log('Our ready called');
        });
    }


}) (unsafeWindow);
于 2013-09-29T00:24:35.907 に答える