Calling a content script function from Plugin

83 views
Skip to first unread message

ikatalan

unread,
Apr 21, 2011, 10:40:05 AM4/21/11
to Chromium-extensions
Hi,

I'm trying to pass a message/Event from my plugin to my contentscript
that loaded that plugin earlier.

NPN_Invoke doesn't seem to work when I try to call a function that is
in my content script.
It works well when I call getElementById or any other 'native' DOM
functions.

I tried using this:

NPN_GetValue(m_pNPInstance, NPNVWindowNPObject, &sWindowObj);

NPVariant npvarRetVal;
NPIdentifier identifier = NPN_GetStringIdentifier( "myFunc" );
NPVariant args;
STRINGZ_TO_NPVARIANT("somearg", args);

if ( !NPN_Invoke(m_pNPInstance, sWindowObj, identifier,&args,
1,&npvarRetVal) )
{
return false;
}

but MyFunc doesn't get called.

what can be the problem?
Am I using it the wrong way?

PhistucK

unread,
Apr 21, 2011, 10:50:05 AM4/21/11
to ikatalan, Chromium-extensions
I would imagine it is a limitation due to the fact that you cannot instantiate the object of the plugin inside the content script, because when you add it to the DOM, it creates an object inside the page, not the content script.
Two workarounds you may employ -
- Pass information using hidden DOM elements and custom events (JSON.stringify and JSON.parse could be your friends here).
- Create the plugin within the background page and use message passing (chrome.extension.sendRequest) to pass information to the content script and back.

The latter applies only if you do not need the plugin to actually show up and be visible/interact-able on the page.

PhistucK




--
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.


ikatalan

unread,
Apr 21, 2011, 11:02:38 AM4/21/11
to Chromium-extensions
Hi Phistuck,

Thanks for the response.

I consider changing the DOM as a last resort action.

I'm working as a background plugin ( no window or any appearance at
all).
So trying to add the plugin to the background seems reasonable.

But When I tried it ( about a week ago ) I could not access the DOM
(security issues) to handle some plugin internal work.

Is it possible to call a background script from the plugin?

plugin --???--> backgroundScript --chrome.tabs.sendrequest-->
contentScript




On Apr 21, 5:50 pm, PhistucK <phist...@gmail.com> wrote:
> I would imagine it is a limitation due to the fact that you cannot
> instantiate the object of the plugin inside the content script, because when
> you add it to the DOM, it creates an object inside the page, not the content
> script.
> Two workarounds you may employ -
> - Pass information using hidden DOM elements and custom events
> (JSON.stringify and JSON.parse could be your friends here).
> - Create the plugin within the background page and use message passing
> (chrome.extension.sendRequest) to pass information to the content script and
> back.
>
> The latter applies only if you do not need the plugin to actually show up
> and be visible/interact-able on the page.
>
> ☆*PhistucK*
>
>
>
>
>
>
>
> On Thu, Apr 21, 2011 at 17:40, ikatalan <ikata...@gmail.com> wrote:
> > Hi,
>
> > I'm trying to pass a message/Event from my plugin to my contentscript
> > that loaded that plugin earlier.
>
> > NPN_Invoke doesn't seem to work when I try to call a function that is
> > in my content script.
> > It works well when I call getElementById or any other 'native' DOM
> > functions.
>
> > I tried using this:
>
> >         NPN_GetValue(m_pNPInstance, NPNVWindowNPObject, &sWindowObj);
>
> >        NPVariant npvarRetVal;
> >        NPIdentifier identifier = NPN_GetStringIdentifier( "myFunc" );
> >        NPVariant args;
> >        STRINGZ_TO_NPVARIANT("somearg", args);
>
> >        if ( !NPN_Invoke(m_pNPInstance, sWindowObj, identifier,&args,
> > 1,&npvarRetVal) )
> >        {
> >                return false;
> >        }
>
> > but MyFunc doesn't get called.
>
> > what can be the problem?
> > Am I using it the wrong way?
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Chromium-extensions" group.
> > 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.

PhistucK

unread,
Apr 21, 2011, 11:33:12 AM4/21/11
to ikatalan, Chromium-extensions
As far as I know, it is possible, just like with any script on a page.
The background page should be no different.
PhistucK



To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.

ikatalan

unread,
Apr 22, 2011, 10:06:42 AM4/22/11
to Chromium-extensions

Thanks, I'll try it as soon as I can


ikatalan



On Apr 21, 6:33 pm, PhistucK <phist...@gmail.com> wrote:
> As far as I know, it is possible, just like with any script on a page.
> The background page should be no different.
Reply all
Reply to author
Forward
0 new messages