0

私はクロム拡張を行っています-バックグラウンドからコンテンツスクリプトインスタンスに関数を渡すにはどうすればよいですか?明らかに関数をシリアル化する必要がありますか?

eval(function_string) を実行しても問題ありませんか?

このようなブードゥー教を練習するのはこれが初めてです。助けを求めて。

4

1 に答える 1

0

これを実現するために使用したコードは次のとおりです。

送信する関数を含むカスタムスクリプト言語スクリプト:

// An example script
// Showing how dependent functions are defined later on
// With functions that have no dependencies grouped together in an associative array
// preceding, in the execution_context_loader's list, the functions that depend on them.

var collection_script.execution_context_loader = [
 {
      fa:function(a) { return a+1; },
      fb:function(b) { return b*2; },
      fc:function(c) { return c-3; }
 },
 {
      fabc:function(a,b,c) {
          return  window.qwontical.execution_context.fa(a)^
                   window.qwontical.execution_context.fb(b)^
                    window.qwontical.execution_context.fc(c);

      }
 }

];

var script_point = {
    name : 'alpha',
    source : '/root/space',
    data_args : [ '/j1', '/j2', '/j3' ],
    function_applied : 'fabc'
};

これを反対側で実行すると(たとえば、何らかの関数を呼び出すことによってexecute("alpha");)、関数fabc(シリアル化および復活)が、、およびで見つかったリソースに適用さ/root/space/j1.../j2ます.../j3

関数送信側:

// Function to package a script's execution context for dispatch to a collection script

function package_script_for_dispatch( collection_script ) {
            console.log(collection_script);
            if ( !!collection_script.execution_context_loader ) {
                    for ( var i = 0; i < collection_script.execution_context_loader.length; i += 1) {
                            for ( var func_def in collection_script.execution_context_loader[i] ) {
                                    collection_script.execution_context_loader[i][func_def] = collection_script.execution_context_loader[i][func_def].toString();
                                    console.log("Packed : " + collection_script.execution_context_loader[i][func_def]);
                            }
                    }
            } else {
                    collection_script.execution_context_loader = {};
            }
    }

機能受信側:

 for( var i = 0; i < collectionscript.execution_context_loader.length; i+= 1) {
            for (var func_name in collectionscript.execution_context_loader[i] ) {
                    var func_def_string = collectionscript.execution_context_loader[i][func_name];
                    console.log("About to revivify " + func_name + " : " + func_def_string);
                    eval("window.qwontical.execution_context['"+func_name+"'] = "+func_def_string);
            }
    }

そのような関数の呼び出し規約は次のようになります

window.qwontical.execution_context[func_name](args...);

これにより、スクリプトで関数を名前で定義し、スクリプトがシリアル化されて「復活」した後、後で使用できるようになります。

于 2013-03-03T17:30:27.960 に答える