copy dom event from background page to popup

60 views
Skip to first unread message

reda

unread,
Dec 18, 2010, 2:28:13 PM12/18/10
to Chromium-extensions
Hi,

Is there any way to copy html content with events created
programmatically from the backgournd page to the popup page.

Example :

In background.html, I have

<div id="test">TEST</div>

Also (in background.html) I have a script that add event to the test
div

//jquery 1.3
$("test").click(function(e){console.log("hello")});

//function called by the popup to retrieve test content (with the
event if possible)
function getTest() {
return $("#test").clone();
}

Now in the popup.html I have :

<div id="test"></div>

and script that append content :
var bkPage = chrome.extension.getBackgroundPage();
$("#test").append(bkPage.getTest());

Obviously I have the html content but no event is attached.

If there is no way to do that in javascript (copy event from a dom to
an other), is there a workaround to my problem because now I'm running
extensive script in the popup itself to prepare the content of my div
and it takes 4 seconds before my popup shows

Thanks for the help.

Arne Roomann-Kurrik

unread,
Dec 20, 2010, 2:16:53 PM12/20/10
to reda, Chromium-extensions
From the documentation of jQuery's clone() method: http://docs.jquery.com/Clone
You need to pass a boolean true argument to clone to clone event handlers.

It's probably still slow though - why not use adoptNode: https://developer.mozilla.org/en/DOM/document.adoptNode ?  It looks like event listeners should be preserved: http://lists.w3.org/Archives/Public/www-dom/2001JulSep/0207.html

~Arne


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


reda

unread,
Dec 24, 2010, 2:58:44 PM12/24/10
to Chromium-extensions
Hello Arne,

Thank you for the reply, I have already tried jquery clone(true)
method but without success.

In the inspector, I can see that the div element that was cloned and
injected in the popup has a click event (in the Event Listeners view)
but when I click it, nothing happens.

BTW, where can I find the onClick core definition (console.log() in
the example below) in the event listener view?

$('div').click(function(){console.log("happy click);});

Because in: Event Listeners -> click -> div -> node -> onclick I have
null


Regarding using adoptNode (or importNode). I have an error in the
console each time I use it:

> document.adoptNode(bgPage.document.getElementById("div"), true);

Uncaught Error: NOT_SUPPORTED_ERR: DOM Exception 9


I'm using Chrome 9.0.597.19 beta on debian lenny (5.0.7)

Is someone else having problem with adoptNode?

Hope someone could explain what I am missing.




On 20 déc, 20:16, Arne Roomann-Kurrik <kur...@chromium.org> wrote:
> From the documentation of jQuery's clone() method:http://docs.jquery.com/Clone
> <http://docs.jquery.com/Clone>You need to pass a boolean true argument to
> clone to clone event handlers.
>
> It's probably still slow though - why not use adoptNode:https://developer.mozilla.org/en/DOM/document.adoptNode?  It looks like
> > To post to this group, send email to chromium-extensi...@chromium.org.
> > To unsubscribe from this group, send email to
> > chromium-extensions+unsubscr...@chromium.org<chromium-extensions%2Bunsubscr...@chromium.org>
> > .
Reply all
Reply to author
Forward
0 new messages