Greasemonkey: Comment appeler une fonction dans un autre script utilisateur Greasemonkey ?

Créé le 28 oct. 2016  ·  8Commentaires  ·  Source: greasemonkey/greasemonkey

Salut,

Je suis nouveau sur Greasemonkey.
Existe-t-il un moyen d'appeler une fonction dans un autre script utilisateur Greasemonkey ?

Merci.

Tous les 8 commentaires

non, à moins que l'autre script ne fasse un effort pour exposer les API par certains moyens.

Il n'y a pas de méthode interne pour le faire, mais il y a une solution de contournement.

Script avec fonction exposée :

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

Script pour appeler la fonction

hello();

C'est l'essentiel.

bien sûr, cela signifie également qu'il peut être appelé depuis le site Web, pas seulement d'autres scripts utilisateur

Vrai. Donnez-lui un paramètre de mot de passe.

Il doit s'agir du même onglet.

// ==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);

Les scripts

@tiansh , dans cet exemple, la protection du bac à sable a été désactivée par <strong i="6">@grant</strong> unsafeWindow .

@pyhedgehog As- tu essayé ? Avec GM 2/3 ?

Cette page vous a été utile?
0 / 5 - 0 notes