2011-02-18 22:53:06 -05:00
|
|
|
#include "WebPage.h"
|
2011-02-25 22:57:55 -05:00
|
|
|
#include "JavascriptInvocation.h"
|
2011-02-25 22:32:22 -05:00
|
|
|
#include <QResource>
|
2011-02-26 13:02:43 -05:00
|
|
|
#include <iostream>
|
2011-02-18 22:53:06 -05:00
|
|
|
|
|
|
|
WebPage::WebPage(QObject *parent) : QWebPage(parent) {
|
2011-02-25 22:32:22 -05:00
|
|
|
connect(mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
|
|
|
|
this, SLOT(injectJavascriptHelpers()));
|
|
|
|
QResource javascript(":/capybara.js");
|
2011-02-26 14:22:38 -05:00
|
|
|
char * javascriptString = new char[javascript.size() + 1];
|
|
|
|
strcpy(javascriptString, (const char *)javascript.data());
|
|
|
|
javascriptString[javascript.size()] = 0;
|
|
|
|
m_capybaraJavascript = javascriptString;
|
2011-03-09 00:08:30 -05:00
|
|
|
m_loading = false;
|
|
|
|
connect(this, SIGNAL(loadStarted()), this, SLOT(loadStarted()));
|
|
|
|
connect(this, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
|
2011-02-25 22:32:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebPage::injectJavascriptHelpers() {
|
|
|
|
mainFrame()->evaluateJavaScript(m_capybaraJavascript);
|
2011-02-18 22:53:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WebPage::shouldInterruptJavaScript() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-25 22:57:55 -05:00
|
|
|
QVariant WebPage::invokeCapybaraFunction(const char *name, QStringList &arguments) {
|
|
|
|
QString qname(name);
|
|
|
|
QString objectName("CapybaraInvocation");
|
|
|
|
JavascriptInvocation invocation(qname, arguments);
|
|
|
|
mainFrame()->addToJavaScriptWindowObject(objectName, &invocation);
|
|
|
|
QString javascript = QString("Capybara.invoke()");
|
|
|
|
return mainFrame()->evaluateJavaScript(javascript);
|
|
|
|
}
|
|
|
|
|
2011-02-25 23:39:29 -05:00
|
|
|
QVariant WebPage::invokeCapybaraFunction(QString &name, QStringList &arguments) {
|
|
|
|
return invokeCapybaraFunction(name.toAscii().data(), arguments);
|
|
|
|
}
|
|
|
|
|
2011-02-26 13:02:43 -05:00
|
|
|
void WebPage::javaScriptConsoleMessage(const QString &message, int lineNumber, const QString &sourceID) {
|
|
|
|
if (!sourceID.isEmpty())
|
|
|
|
std::cout << qPrintable(sourceID) << ":" << lineNumber << " ";
|
|
|
|
std::cout << qPrintable(message) << std::endl;
|
|
|
|
}
|
|
|
|
|
2011-03-09 00:08:30 -05:00
|
|
|
void WebPage::loadStarted() {
|
|
|
|
m_loading = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebPage::loadFinished(bool success) {
|
2011-03-09 18:01:46 -05:00
|
|
|
Q_UNUSED(success);
|
2011-03-09 00:08:30 -05:00
|
|
|
m_loading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebPage::isLoading() const {
|
|
|
|
return m_loading;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString WebPage::failureString() {
|
|
|
|
return QString("Unable to load URL: ") + mainFrame()->url().toString();
|
|
|
|
}
|
|
|
|
|