[Greasemonkey] "Bloglines Keep All New Link" User script

Nikolas Coukouma lists at atrus.org
Tue Jun 14 19:52:24 EDT 2005


Lenny Domnitser wrote:

>On 6/13/05, Mark Wubben <markwubben at gmail.com> wrote:
>  
>
>>On 6/9/05, Jeremy Dunck <jdunck at gmail.com> wrote:
>>    
>>
>>>But the point is, all GM scripts are wrapped, and if you want to
>>>access them outside of the originally injected scope chain, you'll
>>>need to export them.  Put another way: by adding window.foo, you're
>>>guaranteeing that foo is in any further JS's scope chain.
>>>      
>>>
>
>In most cases, exporting functions to window is not necessary. It is
>needed if a link is to javascript:myLocalGreasyFunction(), as in the
>Nicola's Bloglines script, but what I have done in my user scripts is
>to have href="javascript:;" and then set onclick to the local
>function. Here's an example I pulled out of a script of mine:
>
>      var delLink = document.createElement('a');
>      delLink.href = 'javascript:;';
>      delLink.onclick = function() {
>        if(confirm('Delete?')) {
>          deleteClip(href);
>        }
>      }
>
More directly, Javascript is lexically/statically scoped (the scope is
where the function is declared, not where it is called). So, you can
safely use functions within your script's namespace. A simple example:
function doSomething() {
  alert( "two" );
}
someElement.addEventListener( "click", function(){ alert( "one" );
doSomething(); }, false );

-Nikolas Coukouma


More information about the Greasemonkey mailing list