Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. QWebView Javascript to Flash SWF not working
QtWS25 Last Chance

QWebView Javascript to Flash SWF not working

Scheduled Pinned Locked Moved Qt WebKit
2 Posts 1 Posters 2.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jbarnesweb
    wrote on last edited by
    #1

    PACCPL-334LAGYA:flash jbarne200$ cat SwfTest.as
    @
    package {

    import flash.events.*;
    import flash.external.*;
    import flash.display.*;
    
    public class SwfTest extends Sprite {
    
        public function SwfTest():void {
            ExternalInterface.marshallExceptions = true;
            try {
                ExternalInterface.addCallback("createFunction", create);
                ExternalInterface.addCallback("greetFunction", greet);
            } catch (error:Error) {
                trace("error creating callback functions");
            }
        }
    
        public function create():void {
            ExternalInterface.call("receiver.onLog", "createFunction called in actionscript");
            ExternalInterface.call("receiver.onCreated");
        }
    
        public function greet(name:String):void {
            ExternalInterface.call("receiver.onGreeting", "Hello "+name);
        }
    }
    

    }
    @

    I made an html page with the correct object/embed tags that also has javascript with the receiver object defined as having methods per the ExternalInterface.call() functions in actionscript (onLog(), onCreate(), onGreeting()). If I open the page in Firefox, it behaves as expected.

    So far so good...

    PACCPL-334LAGYA:qtswftest jbarne200$ cat main.cpp

    @
    #include <QtGui/QApplication>
    #include "mainwindow.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
    }

    @

    PACCPL-334LAGYA:qtswftest jbarne200$ cat mainwindow.h

    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QtGui/QMainWindow>
    #include <QtWebKit/QWebView>
    #include <QtWebKit/QWebFrame>
    #include <QDebug>

    class JavaScriptObject : public QObject
    {
    Q_OBJECT

    public:
    JavaScriptObject()
    {}

    public slots:
    void onCreated() {
    qDebug() << "onCreated called";
    }

    void onGreeting(QString msg) {
        qDebug() << "Greeting: " << msg;
    }
    
    void onLog(QString msg) {
        qDebug() << "Log: " << msg;
    }
    

    };

    class WebView : public QWebView
    {
    Q_OBJECT

    public:
    WebView(QWidget *parent)
    : QWebView(parent)
    , m_jsObject(new JavaScriptObject)
    , exampleSwf("document.getElementById('swf')")
    {
    QObject::connect(page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), SLOT(bindJavaScriptObject()));
    QObject::connect(this, SIGNAL(loadFinished(bool)), SLOT(onLoadFinished(bool)));
    QObject::connect(this, SIGNAL(swfReady()), SLOT(onSwfReady()));
    }
    ~WebView() {
    delete m_jsObject;
    }

    void init() {
        settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
        settings()->setAttribute(QWebSettings::PluginsEnabled, true);
        QString url = "file:///mnt/nfs/env/swf.html";
        load(QUrl(url));
    }
    
    void callActionScriptMethod(QString method) {
        QString call = exampleSwf + "." + method + "()";
        qDebug() << call;
        QVariant var = page()->mainFrame()->evaluateJavaScript(call);
        qDebug() << "value returned from method call is " << var.toString();
    }
    
    void callActionScriptMethod(QString method, QString params) {
        QString call = exampleSwf+"."+method+"("+params+")";
        qDebug() << call;
        QVariant var = page()->mainFrame()->evaluateJavaScript(call);
        qDebug() << "value returned from callActionScript is " << var.toString();
    }
    

    public slots:
    void bindJavaScriptObject() {
    page()->mainFrame()->addToJavaScriptWindowObject("receiver", m_jsObject);
    qDebug() << "bindJavaScriptObject";
    QString html = page()->mainFrame()->toHtml();
    qDebug() << "html of page is \n" << html;
    emit swfReady();
    }

    void onLoadFinished(bool ok) {
        qDebug() &lt;&lt; "load finished: ok = " << ok;
    }
    
    void onSwfReady() {
        qDebug() << "onSwfReady()";
        callActionScriptMethod("createFunction");
        callActionScriptMethod("greetFunction", "\"Jeff\"");
    }
    

    signals:
    void swfReady();

    private:
    JavaScriptObject *m_jsObject;
    QString exampleSwf;

    };

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    MainWindow(QWidget *parent = 0)
    : QMainWindow(parent)
    , webView(new WebView(this))
    {
    webView->init();
    }
    ~MainWindow() {}
    private:
    WebView *webView;
    };

    #endif // MAINWINDOW_H

    @

    PACCPL-334LAGYA:qtswftest jbarne200$ cat qtswftest.pro
    @

    -------------------------------------------------

    Project created by QtCreator 2013-04-01T09:47:47

    -------------------------------------------------

    QT += webkit gui
    TARGET = qtswftest
    TEMPLATE = app
    SOURCES += main.cpp
    HEADERS += mainwindow.h

    @

    I stripped out the javascript from the swftest.html and saved it to swf.html (per the url in WebView::init()). None of the evaluateJavaScript() calls worked. It seems as though the swf was never loaded? The html was loaded. The browser version of the html works (where the javascript receiver object is embedded in the page).

    Is there a way to tell when a swf gets loaded into webkit? I'm stumped.

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jbarnesweb
      wrote on last edited by
      #2

      Here is the html source that works in firefox.

      @
      PACCPL-334LAGYA:qtswftest jbarne200$ cat /mnt/nfs/env/swftest.html
      <html>
      [removed]

      var swf;
      var receiver;
      function Receiver()
      {

      this.onCreated = onCreated;
      this.onGreeting = onGreeting;
      this.onLog = onLog;

      function onGreeting(msg) {
      alert('Greeting: '+msg);
      }

      function onLog(msg) {
      alert('Log: '+msg);
      }

      function onCreated() {
      alert('onCreated called');
      }
      }

      function getSwf(movieName) {
      if (navigator.appName.indexOf("Microsoft") != -1) {
      return window[movieName]
      }
      else {
      return document[movieName]
      }
      }

      function onBodyLoad() {
      swf = getSwf('SwfTest');
      receiver = new Receiver();
      swf.createFunction();
      swf.greetFunction("Jeff");
      }

      [removed]

      <body marginwidth=0 marginheight=0 >
      <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
      id="SwfTest" width="500" height="375"
      codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">
      <param name="movie" value="SwfTest.swf" />
      <param name="quality" value="high" />
      <param name="bgcolor" value="#869ca7" />
      <param name="allowScriptAccess" value="always" />
      <embed src="SwfTest.swf" quality="high" bgcolor="#869ca7"
      width="500" height="375" name="SwfTest" align="middle"
      play="true" loop="false" quality="high" allowScriptAccess="always"
      type="application/x-shockwave-flash"
      pluginspage="http://www.macromedia.com/go/getflashplayer">
      </embed>
      </object>

      </body>
      </html>
      @

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved