diff --git a/examples/browsermode.js b/examples/browsermode.js new file mode 100644 index 00000000..6745ae9f --- /dev/null +++ b/examples/browsermode.js @@ -0,0 +1,14 @@ +if (phantom.args.length === 0) { + console.log("This example instruct PhantomJS to just open a specific URL.") + console.log('URL must be passed as a parameter.'); + phantom.exit(); +} else { + if (phantom.state.length === 0) { + console.log("Loading: " + phantom.args[0]); + phantom.state = 'gotourl'; + phantom.userAgent = "PhantomJS"; + phantom.open(phantom.args[0]); + } else { + // TODO - I don't have a specific condition for exit yet + } +} diff --git a/examples/browsermode.sh b/examples/browsermode.sh new file mode 100755 index 00000000..b301e985 --- /dev/null +++ b/examples/browsermode.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +PHANTOMJS_EXEC="phantomjs" + +if which $PHANTOMJS_EXEC >/dev/null; then + # Launch PhantomJS passing all the parameters from the CLI + $PHANTOMJS_EXEC `dirname $0`/browsermode.js $@ +else + # Usage + echo "'$PHANTOMJS_EXEC' must be in the \$PATH" +fi diff --git a/src/networkaccessmanager.cpp b/src/networkaccessmanager.cpp new file mode 100644 index 00000000..d4a62e46 --- /dev/null +++ b/src/networkaccessmanager.cpp @@ -0,0 +1,48 @@ +#include +#include + +#include "networkaccessmanager.h" + +// public: +NetworkAccessManager::NetworkAccessManager(QObject *parent) + : QNetworkAccessManager(parent) +{ +} + +// protected: +QNetworkReply *NetworkAccessManager::createRequest(Operation op, const QNetworkRequest & req, QIODevice * outgoingData) +{ + switch(op) { + case QNetworkAccessManager::HeadOperation: { + qDebug() << "HTTP/1.1 HEAD - URL:" << req.url(); + break; + } + case QNetworkAccessManager::GetOperation: { + qDebug() << "HTTP/1.1 GET - URL:" << req.url(); + break; + } + case QNetworkAccessManager::PutOperation: { + qDebug() << "HTTP/1.1 PUT - URL:" << req.url(); + break; + } + case QNetworkAccessManager::PostOperation: { + qDebug() << "HTTP/1.1 POST - URL:" << req.url(); + break; + } + case QNetworkAccessManager::DeleteOperation: { + qDebug() << "HTTP/1.1 DELETE - URL:" << req.url(); + break; + } + case QNetworkAccessManager::CustomOperation: { + qDebug() << "HTTP/1.1 CUSTOM - URL:" << req.url(); + break; + } + default: { + qWarning() << "Unexpected HTTP Operation Type"; + break; + } + } + + // Pass duty to the superclass - Nothing special to do here (yet?) + return QNetworkAccessManager::createRequest(op, req, outgoingData); +} diff --git a/src/networkaccessmanager.h b/src/networkaccessmanager.h new file mode 100644 index 00000000..f73d4fcd --- /dev/null +++ b/src/networkaccessmanager.h @@ -0,0 +1,16 @@ +#ifndef NETWORKACCESSMANAGER_H +#define NETWORKACCESSMANAGER_H + +#include + +class NetworkAccessManager : public QNetworkAccessManager +{ + Q_OBJECT +public: + NetworkAccessManager(QObject *parent = 0); + +protected: + QNetworkReply *createRequest(Operation op, const QNetworkRequest & req, QIODevice * outgoingData = 0); +}; + +#endif // NETWORKACCESSMANAGER_H diff --git a/src/phantom.cpp b/src/phantom.cpp index dadfa07c..ffbabcba 100644 --- a/src/phantom.cpp +++ b/src/phantom.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -24,6 +25,7 @@ Phantom::Phantom(QObject *parent) , m_proxyPort(1080) , m_returnValue(0) , m_converter(0) + , m_netAccessMan(0) { QPalette palette = m_page.palette(); palette.setBrush(QPalette::Base, Qt::transparent); @@ -106,6 +108,10 @@ Phantom::Phantom(QObject *parent) m_args += arg; } + // Provide WebPage with a non-standard Network Access Manager + m_netAccessMan = new NetworkAccessManager(this); + m_page.setNetworkAccessManager(m_netAccessMan); + connect(m_page.mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), SLOT(inject())); connect(&m_page, SIGNAL(loadFinished(bool)), this, SLOT(finish(bool))); @@ -156,7 +162,7 @@ bool Phantom::execute() QFile file; file.setFileName(m_scriptFile); if (!file.open(QFile::ReadOnly)) { - qFatal("Can't open %s\n", qPrintable(m_scriptFile)); + std::cerr << "Can't open '" << qPrintable(m_scriptFile) << "'" << std::endl << std::endl; exit(1); return false; } diff --git a/src/phantom.h b/src/phantom.h index 797ab2e9..1bb44c45 100644 --- a/src/phantom.h +++ b/src/phantom.h @@ -2,6 +2,7 @@ #include "webpage.h" #include "csconverter.h" +#include "networkaccessmanager.h" class Phantom: public QObject { @@ -76,4 +77,5 @@ private: CSConverter *m_converter; QVariantMap m_paperSize; // For PDF output via render() QRect m_clipRect; + NetworkAccessManager *m_netAccessMan; }; diff --git a/src/phantomjs.pro b/src/phantomjs.pro index 4510dfae..95cf063b 100644 --- a/src/phantomjs.pro +++ b/src/phantomjs.pro @@ -12,12 +12,14 @@ HEADERS += csconverter.h \ phantom.h \ webpage.h \ consts.h \ - utils.h + utils.h \ + networkaccessmanager.h SOURCES += phantom.cpp \ webpage.cpp \ main.cpp \ csconverter.cpp \ - utils.cpp + utils.cpp \ + networkaccessmanager.cpp include(gif/gif.pri) diff --git a/src/utils.cpp b/src/utils.cpp index ed07331c..05551e4a 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include "utils.h" @@ -19,18 +20,20 @@ void Utils::showUsage() void Utils::messageHandler(QtMsgType type, const char *msg) { + QDateTime now = QDateTime::currentDateTime(); + switch (type) { case QtDebugMsg: - fprintf(stdout, "[DEBUG]: %s\n", msg); + fprintf(stdout, "%s [DEBUG] %s\n", qPrintable(now.toString(Qt::ISODate)), msg); break; case QtWarningMsg: - fprintf(stderr, "[WARNING]: %s\n", msg); + fprintf(stderr, "%s [WARNING] %s\n", qPrintable(now.toString(Qt::ISODate)), msg); break; case QtCriticalMsg: - fprintf(stderr, "[CRITICAL]: %s\n", msg); + fprintf(stderr, "%s [CRITICAL] %s\n", qPrintable(now.toString(Qt::ISODate)), msg); break; case QtFatalMsg: - fprintf(stderr, "[FATAL]: %s\n", msg); + fprintf(stderr, "%s [FATAL] %s\n", qPrintable(now.toString(Qt::ISODate)), msg); abort(); } }