Merge remote branch 'upstream/master' into cs-examples

1.2
rgieseke 2011-05-26 11:57:29 +02:00
commit 527b25c187
5 changed files with 51 additions and 29 deletions

12
src/bootstrap.js Normal file
View File

@ -0,0 +1,12 @@
// This allows creating a new web page using the construct "new WebPage",
// which feels more natural than "phantom.createWebPage()".
window.WebPage = function() {
var page = phantom.createWebPage();
page.open = function (url, callback) {
this.loadStatusChanged.connect(callback);
this.openUrl(url);
};
return page;
}

View File

@ -40,19 +40,6 @@
#include "utils.h"
#include "webpage.h"
// This allows creating a new web page using the construct "new WebPage",
// which feels more natural than "phantom.createWebPage()".
#define CONSTRUCT_WEBPAGE "window.WebPage = function(){\n" \
"var page = phantom.createWebPage();\n" \
"page.open = function (url, callback) {\n" \
" this.loadStatusChanged.connect(callback);\n" \
" this.openUrl(url);\n" \
"};" \
"return page;" \
"}"
Phantom::Phantom(QObject *parent)
: QObject(parent)
, m_returnValue(0)
@ -152,31 +139,30 @@ Phantom::Phantom(QObject *parent)
m_args += arg;
}
#if 0
// Provide WebPage with a non-standard Network Access Manager
m_netAccessMan = new NetworkAccessManager(this, diskCacheEnabled, ignoreSslErrors);
m_page->setNetworkAccessManager(m_netAccessMan);
#if 0
m_page->settings()->setAttribute(QWebSettings::AutoLoadImages, autoLoadImages);
m_page->settings()->setAttribute(QWebSettings::PluginsEnabled, pluginsEnabled);
m_page->settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, true);
m_page->settings()->setOfflineStoragePath(QDesktopServices::storageLocation(QDesktopServices::DataLocation));
m_page->settings()->setAttribute(QWebSettings::LocalStorageDatabaseEnabled, true);
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
m_page->settings()->setAttribute(QWebSettings::FrameFlatteningEnabled, true);
#endif
#if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0)
m_page->settings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
m_page->settings()->setLocalStoragePath(QDesktopServices::storageLocation(QDesktopServices::DataLocation));
#endif
#endif
m_page->mainFrame()->addToJavaScriptWindowObject("phantom", this);
m_page->mainFrame()->evaluateJavaScript(CONSTRUCT_WEBPAGE);
QFile file(":/bootstrap.js");
if (!file.open(QFile::ReadOnly)) {
qCritical() << "Can not bootstrap!";
exit(1);
}
QString bootstrapper = QString::fromUtf8(file.readAll());
file.close();
if (bootstrapper.isEmpty()) {
qCritical() << "Can not bootstrap!";
exit(1);
}
m_page->mainFrame()->evaluateJavaScript(bootstrapper);
}
QStringList Phantom::args() const
@ -229,7 +215,9 @@ QVariantMap Phantom::version() const
QObject *Phantom::createWebPage()
{
return new WebPage(this);
WebPage *page = new WebPage(this);
page->setNetworkAccessManager(m_netAccessMan);
return page;
}
void Phantom::exit(int code)

View File

@ -3,5 +3,6 @@
<file>phantomjs-icon.png</file>
<file>coffee-script.js</file>
<file>usage.txt</file>
<file>bootstrap.js</file>
</qresource>
</RCC>

View File

@ -34,6 +34,7 @@
#include <math.h>
#include <QApplication>
#include <QDesktopServices>
#include <QDir>
#include <QFileInfo>
#include <QPainter>
@ -98,6 +99,20 @@ WebPage::WebPage(QObject *parent)
m_mainFrame->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);
m_mainFrame->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
m_webPage->settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled, true);
m_webPage->settings()->setOfflineStoragePath(QDesktopServices::storageLocation(QDesktopServices::DataLocation));
m_webPage->settings()->setAttribute(QWebSettings::LocalStorageDatabaseEnabled, true);
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
m_webPage->settings()->setAttribute(QWebSettings::FrameFlatteningEnabled, true);
#endif
#if QT_VERSION >= QT_VERSION_CHECK(4, 6, 0)
m_webPage->settings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
m_webPage->settings()->setLocalStoragePath(QDesktopServices::storageLocation(QDesktopServices::DataLocation));
#endif
// Ensure we have at least document.body.
m_mainFrame->setHtml("<html><body></body></html>");
@ -109,6 +124,11 @@ QWebFrame *WebPage::mainFrame()
return m_mainFrame;
}
void WebPage::setNetworkAccessManager(QNetworkAccessManager *networkAccessManager)
{
m_webPage->setNetworkAccessManager(networkAccessManager);
}
QString WebPage::content() const
{
return m_mainFrame->toHtml();

View File

@ -50,6 +50,7 @@ public:
WebPage(QObject *parent = 0);
QWebFrame *mainFrame();
void setNetworkAccessManager(QNetworkAccessManager *networkAccessManager);
QString content() const;
void setContent(const QString &content);