2011-07-27 1 views
0

NPAPI 플러그인을 쓰고 있습니다. C++ 코드에서는 NPAPI를 사용하여 JS 개체를 만들 수 없습니다.NPAPI를 사용하여 새 JS 객체를 만드는 방법은 무엇입니까?

Foo.js

function Foo(p) 
{ 

    this.a=p; 
} 

Foo.prototype.get=function() 
{ 
    return this.a; 
} 

C++ 코드 (난 그냥 우리가 JS에서 할 수있는 링크 푸 객체를 생성 할 VAR들 = 새로운 푸 :.. 나는이 방법을 시도 (2) 우리는 JS 코드에 jsFoo1 및 jsFoo2을 반환하는 경우)

int newJSObject(const NPP instance,const NPNetscapeFuncs* npnfuncs,const string objectName, 
     const NPVariant* args,const int argsCount,NPVariant& result) 
    { 

     int status; 

     NPObject *winobj; 
     npnfuncs->getvalue(instance,NPNVWindowNPObject,&winobj); 

     NPVariant npJS; 
     NPIdentifier npJSId=npnfuncs->getstringidentifier(objectName.c_str()); 

     status=npnfuncs->getproperty(instance,winobj,npJSId,&npJS); 

     NPObject* npObjJS = NPVARIANT_TO_OBJECT(npJS); 

     NPVariant npPrototype; 
     NPIdentifier npPrototypeId=npnfuncs->getstringidentifier("prototype"); 
     status=npnfuncs->getproperty(instance,npObjJS,npPrototypeId,&npPrototype); 

     NPObject* npObjPrototype = NPVARIANT_TO_OBJECT(npPrototype); 


     NPVariant npJSConstructor; 
     NPIdentifier npJSConstructorId=npnfuncs->getstringidentifier("constructor"); 

     status=npnfuncs->invoke(instance,npObjPrototype,npJSConstructorId, 
     args,argsCount,&npJSConstructor); 

    /* 
    * destroy memory 
    * */ 
     npnfuncs->releaseobject(winobj); 
     npnfuncs->releasevariantvalue(&npJS); 
     npnfuncs->releasevariantvalue(&npJSConstructor); 

     result=npJS; 

     return status; 
    } 




    NPVariant jsFoo1; 
    NPVariant arg; 
    DOUBLE_TO_NPVARIANT(2,arg); 
    NPVariant prams[]={arg}; 
    newJSObject(instance,npnfuncs,"Foo",prams,sizeof(prams)/sizeof(prams[0]),jsFoo); 

    NPVariant jsFoo2; 
    NPVariant arg; 
    DOUBLE_TO_NPVARIANT(3,arg); 
    NPVariant prams[]={arg}; 
    newJSObject(instance,npnfuncs,"Foo",prams,sizeof(prams)/sizeof(prams[0]),jsFoo); 

을하지만, 우리는() Fooxxx.get를 호출합니다. jsFoo1 및 jsFoo2 모두 "생성자"

이 가

사람이 나에게이 NPAPI를 사용하여 C++에서 JS 개체를 만드는 다른 방법을 줄 수 3. 내가 문제가 알고있다?

답변

1

NPN_Construct가 브라우저 개체에서 작동하지 않는 것 같아서 원하는 것을 직접 수행 할 방법이 없습니다. 그러나 당신이 할 수있는 일은 당신을 위해 객체를 생성하고 그것을 반환하는 자바 스크립트 함수를 만드는 것입니다; NPN_Evaluate를 사용하여 자동으로 페이지에 삽입 할 수도 있습니다. 이 같은

뭔가 :

function __create_object_2_params(obj, param1, param2) { 
    return new obj(param1, param2); 
} 

FireBreath 장소에서 일부 유사한 트릭을 사용합니다.

+1

NPN_Evaluate를 사용하여 스크립트를 삽입하면 고마워요. FireBreath의 소스 코드를 읽는 중입니다. 회신을 보내면 ... – greatsea

1

나는 코드 뭐가 잘못 됐는지 모르겠지만, 내가 NPAPI 플러그인을 필요로 할 때, 다음과 같은 프로젝트가 정말 쉽게 만들었 것을 알고 :

FireBreath - "쉽게 만들 수 있습니다 프레임 워크 강력한 브라우저 플러그인 "

nixysa -"NPAPI 플러그인 "에 대한 글루 코드 생성 프레임 워크

당신은 간단한 C를 생성 할 수 있도록 두 프로젝트는, 당신을 위해 모든 노력을 ++ 한 쪽면에 클래스를 추가하고 다른면의 JS 객체로 쉽게 사용할 수 있습니다. 네가 NPAPI에 너무 많이 투자하지 않았다면, 그들에게 시도해 볼 것을 제안한다. 많은 번거 로움을 덜 수 있습니다.

+1

감사합니다. FireBreath의 소스 코드를 다운로드하기 만하면됩니다. 솔직히 말하면, FireBreath는 정말 위대합니다. – greatsea

+0

당신이 좋아하는 것을 기쁘게 생각합니다 ;-) 나는 열심히했습니다. – taxilian