greasemonkey and onclick

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matej

    greasemonkey and onclick

    Hi,

    I try to assign my function to one <A> element in this GM script, but
    it doesn't work as hoped for (after pressing Alt+L in Firefox 1.5
    nothing happens). Does anybody see what I am doing wrong here?

    window.openNewL ocation = function() {
    alert("OK");
    }

    var ATags = document.getEle mentsByTagName( "a");
    var hrefArray = window.location .href.split("/");
    hrefArray[hrefArray.lengt h-1] = "gallery";
    var imageHREF = hrefArray.join( "/");

    // window.open(thi s.href,'extern' ).focus();retur n false"
    // http://cingular.rbmfrontline.com/locations

    for (var i = 0; i < ATags.length; i++)
    {
    tempElem = ATags[i];
    if (tempElem.getAt tribute("href") ==
    "http://cingular.rbmfro ntline.com/locations") {
    tempElem.access key = "L";
    tempElem.onClic k = "function() {openNewLocatio n();return false}";
    GM_log('attribu tes = ' + String(tempElem .onClick));
    }
    };

    Thanks a lot,

    Matej

  • Martin Honnen

    #2
    Re: greasemonkey and onclick



    Matej wrote:

    [color=blue]
    > var ATags = document.getEle mentsByTagName( "a");[/color]
    [color=blue]
    > for (var i = 0; i < ATags.length; i++)
    > {
    > tempElem = ATags[i];
    > if (tempElem.getAt tribute("href") ==
    > "http://cingular.rbmfro ntline.com/locations") {
    > tempElem.access key = "L";[/color]

    The DOM property is named accessKey, see
    <http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-89647724>
    so as always with JavaScript the case matters.
    [color=blue]
    > tempElem.onClic k = "function() {openNewLocatio n();return false}";[/color]

    Case matters again, the event handlers in DOM are all lower case so
    tempElem.onclic k
    is needed. Then you would need to assign a function object and not a
    string e.g.
    tempElem.onclic k = function (evt) {
    openNewLoation( );
    if (typeof evt != 'undefined' && typeof evt.preventDefa ult) {
    evt.preventDefa ult();
    }
    return false;
    };

    However as you say you are writing a GreaseMonkey script then scripting
    onclick is not going to work, see
    <http://diveintogreasem onkey.org/patterns/intercept-clicks.html>
    but you should simple do
    tempElem.addEve ntListener(
    'click',
    function (evt) {
    openNewLoation( );
    evt.preventDefa ult();
    return false;
    },
    false
    );


    --

    Martin Honnen

    Comment

    • Matej

      #3
      Re: greasemonkey and onclick

      You are partially right -- I have to use addEventListene r, but my
      script doesn't work. I will ask for more info on GM list.

      Thanks,

      Matej

      Comment

      • Matej

        #4
        Re: greasemonkey and onclick

        And yes, it was my wrong capitalization of accessKey property.

        Thanks,

        Matej

        Comment

        Working...