Posts that Raffles is monitoring

Subscribe to Posts that Raffles is monitoring 15 posts found

Jul 26, 2008
lazyttrick 40 posts

Topic: For anybody having scope issues with GM_xmlhttpRequest

bump for relevance...

another option is

while(foo){
    request(a, b);
}

function request(a, b){
   GM_xmlhttpRequest({
        method:'GET',
        url:"http://...",
        onload: doIt(a,b)
   });
}

function doIt(a,b){
   //...
}

 
Mar 15, 2008
lazyttrick 40 posts

Topic: Canceling onclick default action when also using ondblclick

My intention was not push up the topic, but add some reference material to future search users (I found this using the search, and it helped me a lot)...

 
Mar 14, 2008
dob 248 posts

Topic: Canceling onclick default action when also using ondblclick

thanks, but pushing a 4 months old topic?

 
Mar 14, 2008
lazyttrick 40 posts

Topic: Canceling onclick default action when also using ondblclick

I just made a similar thing with this Youtube script:
http://userscripts.org/scripts/show/23916

i used event.timeStamp and some other stuff... seems to work nice in my case

 
Nov 21, 2007
Descriptor 757 posts

Topic: Canceling onclick default action when also using ondblclick

That's actually a good idea Joel, it would be easy to insert an "empty" link, then add content after it with some CSS. If I wasn't so dead tired I'd write an example.

 
Nov 20, 2007
Joel H 351 posts

Topic: Canceling onclick default action when also using ondblclick

Raffles,

While the technical subject of how to do this is interesting, I'm going to go somewhat slightly off topic to the wonderful land of UI design and condense the rant into: don't break the paradigm of a link being a link. Add the 'edit this' button/link instead.

-Joel

And as a side note, you did not spell "favorites" correctly :P

 
Nov 18, 2007
Descriptor 757 posts

Topic: Canceling onclick default action when also using ondblclick

Greasemonkey is like the opposite of web development, while you don't want websites to hijack your browser - that's exactly what Greasemonkey is for. Though I may rant on someone when I think they're doing something wrong, I do realize it doesn't matter, I'm really trying to help everyone and learn something myself.

I added some more code above because I realized you can't see what event fires first. theTimer doesn't need to be undefined, it can be 0 and you can just use if(theTimer), but clearing it doesn't change it's value - I used undefined to illustrate that. If you watch the log when you double-click you'll see two "click"s (1 and 2) then a "dblclick" (2).

You might need e.button == 0, etc, if you want access to the context menu to maybe copy an image or copy the link. Like you said, "click" is always fired and e.preventDefault() blocks everything.

 
Nov 18, 2007
Descriptor 757 posts

Topic: Canceling onclick default action when also using ondblclick

Plus - you probably need to check for event.button and event.shiftKey and event.altKey, etc...

 
Nov 18, 2007
Descriptor 757 posts

Topic: Canceling onclick default action when also using ondblclick

You are doing it "backwards", you should use clearTimeout(theTimer) on the second click, just check if e.detail == 2. Example:

var theTimer, ClickSpeed = 400;
function captureClick(e){
  var url = (e.target.href)? e.target.href: e.target.parentNode.href;
  GM_log( e.type + " = " + e.detail + ", " +
      e.target.nodeName + ", " + url );
  if(!url) return;
  e.preventDefault();
  if( e.detail == 1 && theTimer === undefined ){
    theTimer = setTimeout(
        function(e){location.assign(url)}, ClickSpeed);
  }
  else{
    clearTimeout(theTimer);
    theTimer = undefined;
    // function...
  }
}
document.addEventListener("click", captureClick, true);
document.addEventListener("dblclick", captureClick, true);

I added some code to this because slow clicks can cause some problems, and I would think that you wouldn't want to set the timer twice. It might be better to use a separate function for double-click because that should be timed according to the mouse settings (though what about keydown events?). But anyway, you can play with it and see what happens.

You could calculate the time between mousedown and mouseup.

 
Nov 18, 2007
Mikado 570 posts

Topic: Canceling onclick default action when also using ondblclick

There is a logical mistake in what you try to accomplish. If you want single-clicks to navigate the link immediately, this means you want to predict will the user click that link again or won't he. Use timeouts or just try something else, double-clicking isn't cool anyway.

 
Nov 18, 2007
Descriptor 757 posts

Topic: Canceling onclick default action when also using ondblclick

event.detail holds the number of "clicks".

 
Mar 29, 2007
Adrian 44 posts

Topic: For anybody having scope issues with GM_xmlhttpRequest

Johan, that solution is wonderful, as I did notice the potential conflict involving event listeners with my method. That way should work quite well under most circumstances.

 
Mar 28, 2007
Johan Sundström 23 posts

Topic: For anybody having scope issues with GM_xmlhttpRequest

A great resource on the theory and practice of lexical closures and partial application is Brockman's Object-Oriented Event Listening through Partial Application in JavaScript. The title sets out to solve the same kind of issue under another set of circumstances (event listeners), but the solution and all understanding of the problem applies to those circumstances and yours alike. You'll have this problem in many similar situations until you understand how closures work in javascript, and then be rid of the issue.

Another elegant solution proposed there is turning your recipe into

Function.prototype.bind = function( thisObject ) {
  var method = this;
  var oldargs = [].slice.call( arguments, 1 );
  return function () {
    var newargs = [].slice.call( arguments );
    return method.apply( thisObject, oldargs.concat( newargs ));
  };
}

GM_xmlhttpRequest({
  method:'GET',
  url:'http://www.somewhere.com/something.xml',
  onload:myCallback.bind( { myVar:some_variable } )
});

// variables from the {...} literal above are now found in the `this' object:
function myCallback( request ) {
  alert( this.myVar );
  ...
}

This also allows you to pass additional function arguments to myCallback, bound in before the request argument, if you'd like to, perhaps like this:

var url = 'http://www.somewhere.com/something.xml';
GM_xmlhttpRequest({
  method:'GET',
  url:url,
  onload:myCallback.bind( {}, url, some_variable )
});

// 2nd onward argument to bind get prepended to the xhr callback's args:
function myCallback( url, myVar, request ) {
  alert( myVar );
  ...
}

 
Mar 28, 2007
Henrik N 217 posts

Topic: For anybody having scope issues with GM_xmlhttpRequest

That's useful.

Also see http://userscripts.org/forums/1/topics/138#post..., though the above is perhaps the tidier solution. I still need to read up on closures/binding in JS.

 
Mar 28, 2007
Adrian 44 posts

Topic: For anybody having scope issues with GM_xmlhttpRequest

I'm sure quite a few of us have come across the issue of not being able to easily pass variables to time-delayed functions, such as GM_xmlhttpRequest or setTimeout. Especially if these functions are run inside of a recurring function, or inside a loop. So I've created an object class you can use with these functions:

function Scope(o) {
	var scope = this;
	for (a in o)
		this[a] = o[a];
	
	this.callback = function(r) {scope.func.call(scope, r);};
}

This simple class saved me from a lot of headaches with incorrect values.

To use it, simply follow the example below:

// you may use as many variables as you'd like, but `func' MUST be set to your callback function
var scope = new Scope({myVar:some_variable, func:myCallback});

GM_xmlhttpRequest({
  method:'GET',
  url:'http://www.somewhere.com/something.xml',
  onload:scope.callback
});

function myCallback(request) {
  alert(this.myVar); // variables from the Scope object are now found in the `this' object
  ...
}

This works because the variables are copied to the object, thus retaining their current values. And the object will never lose scope as long as the callback function is referenced by the GM_xmlhttpRequest handler.

Enjoy!