4

I'm working on some code to insert a link to a dupe in the SO close question popup:

Close as dupe

What happens when you manually insert a link to a question in the textbox is that some JS kicks in and fetches the question belonging to the URL. However when I insert an URL using javascript (Chrome extension) into the textbox the SO javascript never kicks in to retrieve the question. The code I am using to insert the URL of the dupe is:

$(document).on('click', '.cvhelper-dupelist li', function() {
  var url = $('a', this).attr('href');
  var $dupeBox = $('#duplicate-question');

  $dupeBox.val(url);
});

The above code successfully inserts the link in the textbox, however for some reason the SO javascript never kicks in to retrieve the question.

I've also tried the following to maybe force it to no avail:

$dupeBox.change();

and

$dupeBox.keyup();

To test what is happening you could open up the close as dupe popup (as above) of a random question and run:

$('#duplicate-question').val('http://stackoverflow.com/questions/8028957/headers-already-sent-by-php');

in the browsers console.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • 4
    This question is doomed to forever oscillate between OT closevotes on SO and Meta SO :) – Michael Berkowski Jun 09 '12 at 20:12
  • 1
    @Michael When typing this question I realized that when people were not reading the actual question it would get close voted into oblivion. :-) Funny though we thought of the same thing. Probably it will be true. Only one way to find out :P CV inception? – PeeHaa Jun 09 '12 at 20:15
  • 3
    +1 for exploring the unknown plane of existence between meta and SO proper – Michael Berkowski Jun 09 '12 at 20:17
  • 1
    But, on the bright side, it's unlikely to be migrated to Programmers, or Serverfault. – David Thomas Jun 09 '12 at 20:17
  • The following event handlers are registered to the input field: blur, keydown, keyup, mouseout, paste, submit. Try all and tell us if any worked. – topek Jun 09 '12 at 20:21
  • @topek Tried them all. No luck :( `$('#duplicate-question').change().keydown().keyup().mouseout().paste().submit().blur();`. – PeeHaa Jun 09 '12 at 20:33

1 Answers1

2

I think I figured out one way, but it's rather fragile:

$dupeBox.data('events').keydown[1].handler({keyCode: 13})

This triggers the second keydown event handler of this element. And gives a keyCode for the stubbed event object.

UPDATE

found a better way:

You can construct a jQuery.Event object with:

var e = jQuery.Event("keydown", { keyCode: 64 });

And trigger the event with this object:

$dupeBox.trigger(e)

The problem was that the SO code checked whether there was something in the event object:

e(d).keydown(c).bind("paste", null, function(a) {
  a.which || c(this)
})
topek
  • 18,609
  • 3
  • 35
  • 43
  • +1 However: It works in console, but the same code does not work in my extension (contentscript). :( Any more bright ideas? Code I have tested in both console and in contentscript: `$('#duplicate-question').val('http://stackoverflow.com/questions/8028957/headers-already-sent-by-php'); var e = $.Event('keydown', { keyCode: 64 }); $('#duplicate-question').trigger(e);` – PeeHaa Jun 09 '12 at 21:29
  • I'm no expert in content scripts, but I can give you a hint how to debug: go to the Sources panel of your Chrome Dev Tools, open stub.js (cdn.sstatic.net/js), use the {} to unobfuscate the source and set a breakpoint on line 580 `a.which || c(this)`. This is the line where the key up event / paste event handler is triggered. – topek Jun 09 '12 at 21:40
  • $dupBox.trigger('paste', {which:13}) would be a possible solution, too. But I doubt that it is any better than the above. – topek Jun 09 '12 at 21:46
  • a contentscript is just some extra JS file injected into a page. That's why I think it is strange it doesn't work. Will be looking further into it. – PeeHaa Jun 09 '12 at 22:03