Greasemonkey: 別のGreasemonkeyユーザースクリプトで関数を呼び出す方法は?

作成日 2016年10月28日  ·  8コメント  ·  ソース: greasemonkey/greasemonkey

やあ、

私はGreasemonkeyを初めて使用します。
別のGreasemonkeyユーザースクリプトで関数を呼び出す方法はありますか?

ありがとう。

全てのコメント8件

いいえ、そうではありません。他のスクリプトが何らかの手段でAPIを公開するために何らかの努力をしている場合を除きます。

それを行うための内部メソッドはありませんが、回避策はあります。

公開された関数を持つスクリプト:

unsafeWindow.hello = hello;
function hello() {alert("hello world");}

関数を呼び出すスクリプト

hello();

それがその要点です。

もちろん、それは他のユーザースクリプトだけでなく、ウェブサイトからも呼び出すことができることを意味します

NS。 パスワードパラメータを指定します。

同じタブである必要があります。

// ==UserScript==
// <strong i="5">@name</strong>        Test call 1
// <strong i="6">@namespace</strong>   https://github.com/tiansh/
// <strong i="7">@include</strong>     http://example.com/
// <strong i="8">@version</strong>     1
// <strong i="9">@grant</strong>       unsafeWindow
// ==/UserScript==

unsafeWindow.x = function (str, callback) {
  console.log('%o.x: %o', this, arguments);
  alert('x says: ' + str);
  callback(str + 'from x');
};
// ==UserScript==
// <strong i="12">@name</strong>        Test call 2
// <strong i="13">@namespace</strong>   https://github.com/tiansh/
// <strong i="14">@include</strong>     http://example.com/
// <strong i="15">@version</strong>     1
// <strong i="16">@grant</strong>       unsafeWindow
// ==/UserScript==

setTimeout(function () {

unsafeWindow.x('hello', function (nstr) {
  console.log('%o.y: %o', this, arguments);
  alert('y says: ' + nstr);
});

}, 0);

ウェブサイトのページにある@ the8472スクリプトは、サンドボックス保護のために関数を呼び出すことができません。

@tiansh 、この例では、サンドボックス保護は<strong i="6">@grant</strong> unsafeWindowによって無効にされました。

@pyhedgehog試しましたか? GM 2/3で?

このページは役に立ちましたか?
0 / 5 - 0 評価