[Greasemonkey] New GM_ function request.

Adam Judson adamsplugins at gmail.com
Wed Aug 24 12:35:05 EDT 2005


On 24/08/05, Adam Judson <adamsplugins at gmail.com> wrote:
> On 23/08/05, Nikolas Coukouma <lists at atrus.org> wrote:
> > As I said, the solution is more general,
> 
> I agree a more general solution is better.  And I suspect "use my
> extension from GM" is
> not an uncommon request.  Also, given the amount of cut and paste in
> most scripts, a
> library of reusable script elements (packaged as an extension) seems desirable.
> 
> > if( export_to_GM ) {
> >   export_to_GM("tamperdata_open", someFunc);
> > }
> >
> 
> I think I'm still missing something.
> 


Okay, scratch that, I figured it out.

That solution is MUCH better.  So can I talk someone into putting this in?

Locally I implemented it as:

browser.js

GM_BrowserUI.init = function() {
...
   this.exportedFunctions = {};
...

GM_BrowserUI.exportToGM = function(funktion, name) {
   if (funktion && name) {
      if (!this.exportedFunction[name]) {
         this.exportedFunctions[name] = funktion;
      }
   }
}

GM_BrowserUI.onDocStart = function(unsafeWin, unsafeTop, isFile) {
...
  new GM_DocHandler(unsafeWin, window, commander, isFile,
this.exportedFunctions);
...  


dochandler.js

function GM_DocHandler(unsafeContentWin, chromeWindow, menuCommander,
isFile, exportedFunctions) {
...
   this.exportedFunctions = exportedFunctions;
...

GM_DocHandler.prototype.injectScript = function(script) {
...
  sandbox.GM_xmlhttpRequest = GM_hitch(xmlhttpRequester, 
                                       "contentStartRequest");

   // do the exported functions
   for (var efn in this.exportedFunctions) {
      sandbox[efn] = this.exportedFunctions[efn];
   }
...



then in my overlay which is injected into browser.xul:

      function TamperDataExportToGM() {
         // try to export some functions to greasemonkey
         if (GM_BrowserUI != undefined && GM_BrowserUI.exportToGM !=
undefined) {
            GM_BrowserUI.exportToGM(TamperGui.modifyUri, "GM_TD_ModifyUri");
         } else {
         }
      }
      setTimeout("TamperDataExportToGM()", 1000);

if I didn't use setTimeout, the GM_BrowserUI var was always undefined,
probably to do with the ordering of overlays into browser.xul, but I'm
not sure.

I'm not sure I understand the restrictions that running inside the
sandbox creates, are there
things that won't work inside my functions?

Adam


More information about the Greasemonkey mailing list