mirror of
https://github.com/ariya/phantomjs.git
synced 2024-09-17 15:47:09 +03:00
New "shim" Network Access Manager, new 'browsermode' example and more.
The new Network Access Manager currently is designed to do just logging: - logs HTTP Method type - logs HTTP Destination URL The new 'browsermode': It's in "/examples" and make PhantomJS do just a call to "open(url)". It has a companion shell script to help with the task. Also, improvement for Message Handler: messages are now prefixed with ISO Datetime.
This commit is contained in:
parent
c616205620
commit
6ec8b3b669
14
examples/browsermode.js
Normal file
14
examples/browsermode.js
Normal file
@ -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
|
||||
}
|
||||
}
|
11
examples/browsermode.sh
Executable file
11
examples/browsermode.sh
Executable file
@ -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
|
48
src/networkaccessmanager.cpp
Normal file
48
src/networkaccessmanager.cpp
Normal file
@ -0,0 +1,48 @@
|
||||
#include <QDebug>
|
||||
#include <QNetworkRequest>
|
||||
|
||||
#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);
|
||||
}
|
16
src/networkaccessmanager.h
Normal file
16
src/networkaccessmanager.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef NETWORKACCESSMANAGER_H
|
||||
#define NETWORKACCESSMANAGER_H
|
||||
|
||||
#include <QNetworkAccessManager>
|
||||
|
||||
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
|
@ -1,3 +1,4 @@
|
||||
#include <iostream>
|
||||
#include <QDebug>
|
||||
|
||||
#include <gifwriter.h>
|
||||
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
};
|
||||
|
@ -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)
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include <iostream>
|
||||
#include <QFile>
|
||||
#include <QDebug>
|
||||
#include <QDateTime>
|
||||
|
||||
#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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user