1

This is a follow up question on this question. Short version is: I'm trying to insert a dupe link (close vote as dupe here on Stack Overflow) into the textfield in the close vote popup using javascript (Chrome extension). The previous answer got me a bit further and seems the solutions. However the answer on that question works in console, but doesn't work in my chrome extension for some reason.

The problem is: when opening the vote to close popup here on Stack Overflow on a question and clicking the dupe reason. I can run the following code in the console:

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

var e = $.Event('keydown', { keyCode: 64 });
$dupeQuestion.trigger(e);

And as already mentioned this works. The link is inserted into the textbox and the Stack Overflow JS kicks in to retrieve the question. However when I try to run that exact same code in my extension it only kinda works:

$(document).on('click', '.cvhelper-dupelist li', function() {
  var $dupeQuestion = $('#duplicate-question');
  $dupeQuestion.val('https://stackoverflow.com/questions/8028957/headers-already-sent-by-php');

  var e = $.Event('keydown', { keyCode: 64 });
  $dupeQuestion.trigger(e);
});

Kinda works means: the link is inserted in the textbox, but the Stack Overflow script to retrieve the question doesn't kick in. So I am totally clueless what could be wrong. Perhaps there is a difference somewhere when using console vs contentscript? Anything else?

I've created a download with a very simplified version of the extension at github.

Community
  • 1
  • 1
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • maybe you are overwriting stackoverflows javascript, try to use setTimeout for testing it, because console script is executed in a insulated area, i think. – Tearsdontfalls Jun 09 '12 at 23:15
  • 1
    Is it important that the `console`-run script all be one line in the question? It makes it kinda hard to read and compare against the other. Also, I didn't know you could use `$.on()` with `document`. Not saying you can't, just didn't know you could. – Jared Farrish Jun 09 '12 at 23:17
  • Yes, you can also try to replace that with $(document).ready(function (){$('.cvhelpeler-dupelist li').click(function(){});} – Tearsdontfalls Jun 09 '12 at 23:19
  • @RepWhoringPeeHaa - Just for you to know, you can type in lines in the Developer tools (console) by pressing Shift + Enter. – Derek 朕會功夫 Jun 09 '12 at 23:23
  • @JaredFarrish You can run it on document. And the one-liner is just for easy copy-pasting in console to test. – PeeHaa Jun 09 '12 at 23:38
  • @Tearsdontfalls That's not what `on()` does. – PeeHaa Jun 09 '12 at 23:38
  • @JaredFarrish I've updated my question with a download link to the simplified extension which may help. – PeeHaa Jun 09 '12 at 23:50
  • Do you have a link to the project on github? – Jared Farrish Jun 09 '12 at 23:51
  • @JaredFarrish lemme push the latest changes – PeeHaa Jun 10 '12 at 00:07
  • @JaredFarrish [Stable](https://github.com/PeeHaa/cv-pls) and [dev](https://github.com/PeeHaa/cv-pls/tree/dev) (the one with the issue). And [this](https://github.com/downloads/PeeHaa/cv-pls/cv-pls.zip) is the simplified extension with the issue. – PeeHaa Jun 10 '12 at 00:12
  • Ok, great. I'll take a look, I've been wanting to take a look at Chrome extensions as it is. – Jared Farrish Jun 10 '12 at 00:34
  • @JaredFarrish If you need more info about my issue just say so. And if you need more info about extensions in general just drop me a line in [php chat](http://chat.stackoverflow.com/rooms/11/php). – PeeHaa Jun 10 '12 at 00:35
  • *Chrome extension to help cleanup Stack Overflow.* - That made me chuckle. I'll be interested in taking a look at it. At least through the main app channels, I've been surprised there aren't more SO-related apps available. Stackapps notwithstanding. I'm also seeign if your on chat, I have a question about using git. – Jared Farrish Jun 10 '12 at 00:39
  • I can't find the `.cvhelper-dupelist` element on stackoverflow pages... – Derek 朕會功夫 Jun 10 '12 at 00:44
  • @Derek That's because it is added by my extension. – PeeHaa Jun 10 '12 at 00:51
  • @Derek basically what happens is that I add a list with possible dupes and the user selects a dupe and it should be inserted into the textbox – PeeHaa Jun 10 '12 at 01:05

2 Answers2

0

One of the possible reasons is that your Content Script is executed before other scripts load and it messed up other things. If it is the reason, then try:

"content_scripts": [
    {
        "run_at": "document_end",
        "js": ["jquery.js", "myscript.js"]
    }
]

It might work, but I really don't know. :/ Try it and see if it fixes.

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
0

Okay, this is the only thing that would come to mind:

$(document).on('click', '.cvhelper-dupelist li', function() {
  var $dupeQuestion = $('#duplicate-question');
  $dupeQuestion.val('http://stackoverflow.com/questions/8028957/headers-already-sent-by-php');

  setTimeout(function() {
      var e = $.Event('keydown', { keyCode: 64 });
      $dupeQuestion.trigger(e);
  }, 1);
});

It would basically let the click handler finish before attempting to trigger the keyDown.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309