I am new to GM, so maybe just need some tip&trick..

I want to extend functionality of page, which uses jquery. To get reference to $ object, I have found some workaround, but I have lost reference to GM:

(function(fn){
  console.log('jquery: ', $); // this lead to reference error
  console.log('GM: ', GM); // this is OK, I have the reference
  var newScript = document.createElement('script');
  newScript.setAttribute("type", "application/javascript");
  newScript.textContent = '(' + fn + ')();';
  document.body.appendChild(newScript);
  newScript.parentNode.removeChild(newScript);
})(function(){
  console.log('jquery: ', $); // this is OK, I can work with jQuery
  console.log('GM: ', GM); // this lead to reference error
})();

How to write proper GM script so I can use jQuery and GM object as well? I am not saying that the example written above is perfect, if you have diferent solution, no problem.

BTW: As a bonus, can you please explain why references to GM and $ behave like this? I dont understand it very well.

Thank you

0 Answers