Skip to content
This repository was archived by the owner on May 30, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 66 additions & 6 deletions src/phantomjs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <QDebug>
#include <QtGui>
#include <QtWebKit>
#include <iostream>
Expand All @@ -44,6 +45,17 @@
#define PHANTOMJS_VERSION_PATCH 0
#define PHANTOMJS_VERSION_STRING "1.1.0"


#define JS_MOUSEEVENT_CLICK_WEBELEMENT "(function (target) { " \
"var evt = document.createEvent('MouseEvents');" \
"evt.initMouseEvent(\"click\", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" \
"target.dispatchEvent(evt);" \
"})(this);"
#define JS_INCLUDE_SCRIPT_TAG "var el = document.createElement('script');" \
"el.onload = %2 || null;" \
"el.src = '%1';" \
"document.body.appendChild(el);"

#define PHANTOMJS_PDF_DPI 72 // Different defaults. OSX: 72, X11: 75(?), Windows: 96

void showUsage()
Expand All @@ -67,6 +79,10 @@ class WebPage: public QWebPage
public slots:
bool shouldInterruptJavaScript();

private slots:
void handleFrameUrlChanged(const QUrl &url);
void handleLinkClicked(const QUrl &url);

protected:
void javaScriptAlert(QWebFrame *originatingFrame, const QString &msg);
void javaScriptConsoleMessage(const QString &message, int lineNumber, const QString &sourceID);
Expand All @@ -84,6 +100,16 @@ WebPage::WebPage(QObject *parent)
: QWebPage(parent)
{
m_userAgent = QWebPage::userAgentForUrl(QUrl());
connect(this->currentFrame(), SIGNAL(urlChanged(QUrl)), this, SLOT(handleFrameUrlChanged(QUrl)));
connect(this, SIGNAL(linkClicked(QUrl)), this, SLOT(handleLinkClicked(QUrl)));
}

void WebPage::handleFrameUrlChanged(const QUrl &url) {
qDebug() << "URL Changed: " << qPrintable(url.toString());
}

void WebPage::handleLinkClicked(const QUrl &url) {
qDebug() << "URL Clicked: " << qPrintable(url.toString());
}

void WebPage::javaScriptAlert(QWebFrame *originatingFrame, const QString &msg)
Expand Down Expand Up @@ -167,6 +193,9 @@ public slots:
void exit(int code = 0);
void open(const QString &address);
void setFormInputFile(QWebElement el, const QString &fileTag);
void simulateMouseClick(const QString &selector);
bool loadJs(const QString &jsFilePath);
void includeJs(const QString &jsFilePath, const QString &callback = "undefined");
bool render(const QString &fileName);
void sleep(int ms);

Expand Down Expand Up @@ -373,9 +402,10 @@ QString Phantom::loadStatus() const

void Phantom::open(const QString &address)
{
qDebug() << "Opening address: " << qPrintable(address);
m_page.triggerAction(QWebPage::Stop);
m_loadStatus = "loading";
m_page.mainFrame()->setUrl(address);
m_page.mainFrame()->load(address);
}

bool Phantom::render(const QString &fileName)
Expand Down Expand Up @@ -447,13 +477,43 @@ void Phantom::sleep(int ms)
void Phantom::setFormInputFile(QWebElement el, const QString &fileTag)
{
m_page.m_nextFileTag = fileTag;
el.evaluateJavaScript("(function(target){ \
var evt = document.createEvent('MouseEvents'); \
evt.initMouseEvent(\"click\", true, true, window, \
0, 0, 0, 0, 0, false, false, false, false, 0, null); \
target.dispatchEvent(evt);})(this);");
el.evaluateJavaScript(JS_MOUSEEVENT_CLICK_WEBELEMENT);
}

void Phantom::simulateMouseClick(const QString &selector) {
// Execute the equivalent of "querySelectorAll"
QWebElementCollection webElements = m_page.currentFrame()->findAllElements(selector);
// Click on every one of the elements
foreach ( QWebElement el, webElements ) {
qDebug() << "Element Clicked Center Position: " << el.geometry().center().x() << "," << el.geometry().center().y();
el.evaluateJavaScript(JS_MOUSEEVENT_CLICK_WEBELEMENT);
}
}

bool Phantom::loadJs(const QString &jsFilePath) {
qDebug() << "Loading JS File: " << jsFilePath;
if ( !jsFilePath.isEmpty()) {
QFile jsFile;

jsFile.setFileName(jsFilePath);
if ( !jsFile.open(QFile::ReadOnly) ) {
qWarning() << "Can't load Javascript File: " << qPrintable(jsFilePath);
} else {
QString script = QString::fromUtf8(jsFile.readAll());
jsFile.close();
// Execute JS code in the context of the document
m_page.mainFrame()->evaluateJavaScript(script);

return true;
}
}
return false;
}

void Phantom::includeJs(const QString &jsFilePath, const QString &callback) {
qDebug() << "Including JS File:" << jsFilePath << "- Callback:" << callback;
m_page.mainFrame()->evaluateJavaScript(QString(JS_INCLUDE_SCRIPT_TAG).arg(jsFilePath, callback));
}

void Phantom::setState(const QString &value)
{
Expand Down
1 change: 1 addition & 0 deletions src/phantomjs.pro
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ SOURCES = phantomjs.cpp csconverter.cpp
RESOURCES = phantomjs.qrc
QT += network webkit
CONFIG += console
DEFINES += QT_NO_DEBUG_OUTPUT

include(gif/gif.pri)

Expand Down