WebKit-QT Javascript

Source: Internet
Author: User

Open the following file in the QT directory:
/Src/3 rdparty/WebKit/javascriptcore/bindings/runtime. cpp

There are the following lines:

View plaincopy to clipboardprint?

  1. # If platform (QT)
  2. Case instance: qtlanguage :{
  3. Newinstance = bindings: qtinstance: getqtinstance (qobject *) nativeinstance, rootobject );
  4. Break;
  5. }
  6. # Endif

# If platform (QT) Case instance: qtlanguage: {newinstance = bindings: qtinstance: getqtinstance (qobject *) nativeinstance, rootobject); break ;}# endif

This indicates that you can use qtlanguage to write custom JavaScript objects. There are also a few other languages, that is, you can use them to write extended JavaScript objects, and you should be able to customize your own language. Since the qt gui is used, qtlanguage should be the most suitable.

Open the example file:
/Src/3 rdparty/WebKit/javascriptcore/bindings/testqtbindings. cpp

You can see the following lines:

View plaincopy to clipboardprint?
  1. Static char code [] =
  2. "Myinterface. Foo ();/N"
  3. "Myinterface. teststring =/" Hello/";/N"
  4. "Str = myinterface. teststring;/N"
  5. "Myinterface. testint = 10;/N"
  6. "I = myinterface. testint;/N ";

Static char code [] = "myinterface. foo ();/N "" myinterface. teststring =/"Hello/";/N "" str = myinterface. teststring;/N "" myinterface. testint = 10;/N "" I = myinterface. testint;/N ";

This means that the content of testqtbindings. cpp can parse the following javascript:

View plaincopy to clipboardprint?
  1. Myinterface. Foo ();
  2. Myinterface. teststring = "hello ";
  3. VaR STR = myinterface. teststring;
  4. Myinterface. testint = 10;
  5. VaR I = myinterface. testint;

Myinterface. Foo (); myinterface. teststring = "hello"; var STR = myinterface. teststring; myinterface. testint = 10; var I = myinterface. testint;

I want to define my own Javascript. For example, add a csdn object before the myinterface object:

View plaincopy to clipboardprint?
  1. Csdn. myinterface. Foo ();
  2. Csdn. myinterface. teststring = "hello ";
  3. VaR STR = csdn. myinterface. teststring;

Csdn. myinterface. Foo (); csdn. myinterface. teststring = "hello"; var STR = csdn. myinterface. teststring;

This is simple, but it is like csdn. myinterface. in this way, I did not find the connection between the csdn and myinterface objects on the Internet (Chinese and English websites) for a long time. I thought of the result myself.

1. Add another mycsdnobject object to the original file:

View plaincopy to clipboardprint?
  1. Class mycsdnobject: Public qobject
  2. {
  3. Q_object
  4. Q_property (qstring teststring read teststring write setteststring)
  5. Q_property (INT testint read testint write settestint)
  6. // Add the qobject member as the property of the csdn object and map it to the testmyinterface function.
  7. Q_property (qobject * myinterface read testmyinterface)
  8. Public:
  9. Mycsdnobject (): qobject (0), INTEGER (0), m_myobject (null ){}
  10. Void setteststring (const qstring & Str ){
  11. Qdebug () <"called setteststring" <STR;
  12. String = STR;
  13. }
  14. Void settestint (int I ){
  15. Qdebug () <"called settestint" <I;
  16. Integer = I;
  17. }
  18. Qstring teststring () const {
  19. Qdebug () <"called teststring" <string;
  20. Return string;
  21. }
  22. Int testint () const {
  23. Qdebug () <"called testint" <integer;
  24. Return integer;
  25. }
  26. // A qobject object is returned here.
  27. Qobject * testmyinterface ()
  28. {
  29. If (m_myobject = NULL)
  30. M_myobject = new myobject ();
  31. Return m_myobject;
  32. }
  33. Qstring string;
  34. Int integer;
  35. // Name a qobject object
  36. Myobject * m_myobject;
  37. Public slots:
  38. Void Foo () {qdebug () <"foo invoked ";}
  39. };

Class mycsdnobject: Public qobject {q_object q_property (qstring teststring read teststring write setteststring) q_property (INT testint read testint write settestint) // Add the qobject member as the property of the csdn object, ing to the testmyinterface function q_property (qobject * myinterface read testmyinterface) Public: mycsdnobject (): qobject (0), INTEGER (0), m_myobject (null) {} void setteststring (const qstring & Str) {qdebug () <"called setteststring" <STR; string = STR;} void settestint (int I) {qdebug () <"called settestint" <I; integer = I;} qstring teststring () const {qdebug () <"called teststring" <string; return string ;} int testint () const {qdebug () <"called testint" <integer; return integer;} // here, a qobject object qobject * testmyinterface () is returned () {If (m_myobject = NULL) m_myobject = new myobject (); Return m_myobject;} qstring string; int integer; // name a qobject object myobject * m_myobject; Public slots: void Foo () {qdebug () <"foo invoked ";}};

In this way, two objects, mycsdnobject and myobject, can be connected to csdn. testmyinterface.

2. Modify the name of the created root object.

View plaincopy to clipboardprint?

  1. Global-> put (exec, identifier ("csdn"), instance: createruntimeobject (instance: qtlanguage, (void *) myobject ));

Global-> put (exec, identifier ("csdn"), instance: createruntimeobject (instance: qtlanguage, (void *) myobject ));

 
In this way, you can. This is a test program. You need to add the test code to the WebKit browser. Open the file:
./Src/3 rdparty/WebKit/QT/API/qwebframe. h
Name the myobject class and mycsdnobject class, and then add the csdn root object as a private member of the qwebframe class, as shown below:

View plaincopy to clipboardprint?
  1. Class myobject: Public qobject
  2. {
  3. Q_object
  4. // Omit...
  5. };
  6. Class mycsdnobject: Public qobject
  7. {
  8. Q_object
  9. // Omit...
  10. };
  11. Class qwebkit_export qwebframe: Public qobject
  12. {
  13. Q_object
  14. Q_property (qreal textsizemultiplier read textsizemultiplier write settextsizemultiplier)
  15. Q_property (qstring title Read title)
  16. Q_property (qurl URL read URL write seturl)
  17. Q_property (qicon icon read icon)
  18. Q_property (qsize contentssize read contentssize)
  19. PRIVATE:
  20. Qwebframe (qwebpage * parent, qwebframedata * framedata );
  21. Qwebframe (qwebframe * parent, qwebframedata * framedata );
  22. ~ Qwebframe ();
  23. // Name csdn root object
  24. Mycsdnobject * m_csdn;
  25. Public:
  26. // The following content is omitted...
  27. };

Class myobject: Public qobject {q_object // omitting ...}; class mycsdnobject: Public qobject {q_object // omitting ...}; class qwebkit_export qwebframe: Public qobject {q_object q_property (qreal textsizemultiplier read writable write failed) q_property (qstring title Read title) q_property (qurl URL read URL write seturl) q_property) q_property (qsize contentssize R EAD contentssize) PRIVATE: qwebframe (qwebpage * parent, qwebframedata * framedata); qwebframe (qwebframe * parent, qwebframedata * framedata );~ Qwebframe (); // The root object mycsdnobject * m_csdn; public: // The following is omitted ...};

Open the qwebframe. cpp file and create the csdn root object in the qwebframe constructor.

View plaincopy to clipboardprint?
  1. Qwebframe: qwebframe (qwebpage * parent, qwebframedata * framedata)
  2. : Qobject (parent)
  3. , D (New qwebframeprivate)
  4. {
  5. D-> page = parent;
  6. D-> Init (this, parent-> D-> page, framedata );
  7. // Create the csdn root object here
  8. {
  9. KJS: bindings: rootobject * root = D-> frame-> bindingrootobject ();
  10. KJS: execstate * exec = root-> interpreter ()-> globalexec ();
  11. KJS: jsobject * Global = root-> interpreter ()-> globalobject ();
  12. M_csdn = new mycsdnobject ();
  13. Global-> put (exec, KJS: identifier ("csdn"), KJS: bindings: instance: createruntimeobject (KJS: bindings: instance: qtlanguage, (void *) m_csdn, root ));
  14. }
  15. If (! Framedata-> URL. isempty ()){
  16. Resourcerequest request (framedata-> URL, framedata-> referrer );
  17. D-> frame-> loader ()-> load (request, framedata-> name );
  18. }
  19. }

Qwebframe: qwebframe (qwebpage * parent, qwebframedata * framedata): qobject (parent), D (New qwebframeprivate) {d-> page = parent; D-> Init (this, parent-> D-> page, framedata); // create the csdn root object {KJS: bindings: rootobject * root = D-> frame-> bindingrootobject (); KJS: execstate * exec = root-> interpreter ()-> globalexec (); KJS: jsobject * Global = root-> interpreter ()-> globalobject (); m_csdn = new mycsdnobject ( ); Global-> put (exec, KJS: identifier ("csdn"), KJS: bindings: instance: createruntimeobject (KJS: bindings: instance :: qtlanguage, (void *) m_csdn, root);} If (! Framedata-> URL. isempty () {resourcerequest request (framedata-> URL, framedata-> referrer); D-> frame-> loader ()-> load (request, framedata-> name );}}

Delete the csdn root object in the destructor of qwebframe

View plaincopy to clipboardprint?
  1. Qwebframe ::~ Qwebframe ()
  2. {
  3. If (D-> frame & D-> frame-> loader ()-> client ())
  4. Static_cast (D-> frame-> loader ()-> client ()-> m_webframe = 0;
  5. If (m_csdn)
  6. {
  7. Delete m_csdn;
  8. M_csdn = NULL;
  9. }
  10. Delete D;
  11. }

Qwebframe ::~ Qwebframe () {If (D-> frame & D-> frame-> loader ()-> client ()) static_cast (D-> frame-> loader ()-> client ()-> m_webframe = 0; If (m_csdn) {Delete m_csdn; m_csdn = NULL;} Delete D ;}

Then, write an HTML test page csdn1.htm. The content is as follows:

<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> new document </title>
<Script language = "JavaScript">
<! --
Function Test ()
{
Csdn. myinterface. Foo ();
Csdn. myinterface. teststring = "Hello qtwebkit JavaScript ";
VaR STR = csdn. myinterface. teststring;
Document. getelementbyid ("txtmessage"). innerhtml = STR;
}
// -->
</SCRIPT>
</Head>

<Body>
<P> <a href = "javascript: Test ();"> test QT-WebKit JavaScript </a> </P>
<P id = "txtmessage"> </P>
</Body>
</Html>

Compile and run the WebKit browser, open the test page csdn1.htm, and click the test QT-WebKit JavaScript link above to get the following results:

The above Hello qtwebkit Javascript is the content of csdn. myinterface. teststring set by JavaScript.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.