Skip to content
This repository was archived by the owner on May 30, 2023. It is now read-only.

File Uploads #6

Merged
1 commit merged into from
Feb 9, 2011
Merged
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
36 changes: 36 additions & 0 deletions src/phantomjs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,12 @@ public slots:
void javaScriptAlert(QWebFrame *originatingFrame, const QString &msg);
void javaScriptConsoleMessage(const QString &message, int lineNumber, const QString &sourceID);
QString userAgentForUrl(const QUrl &url) const;
QString chooseFile (QWebFrame * parentFrame, const QString & suggestedFile );

private:
QString m_userAgent;
QMap<QString, QString> m_allowedFiles;
QString m_nextFileTag;
friend class Phantom;
};

Expand Down Expand Up @@ -90,6 +93,15 @@ QString WebPage::userAgentForUrl(const QUrl &url) const
return m_userAgent;
}

QString WebPage::chooseFile (QWebFrame * parentFrame, const QString & suggestedFile )
{
Q_UNUSED(parentFrame);
Q_UNUSED(suggestedFile);
if (m_allowedFiles.contains(m_nextFileTag))
return m_allowedFiles.value(m_nextFileTag);
return NULL;
}

class Phantom: public QObject
{
Q_OBJECT
Expand Down Expand Up @@ -128,6 +140,7 @@ class Phantom: public QObject
public slots:
void exit(int code = 0);
void open(const QString &address);
void setFormInputFile(QWebElement el, const QString &fileTag);
bool render(const QString &fileName);
void sleep(int ms);

Expand Down Expand Up @@ -157,6 +170,18 @@ Phantom::Phantom(QObject *parent)
m_args = QApplication::arguments();
m_args.removeFirst();
m_args.removeFirst();

QStringListIterator argIterator(m_args);
while (argIterator.hasNext()) {
const QString &arg = argIterator.next();
if (arg.startsWith("--upload-file") && argIterator.hasNext()) {
const QString &fileInfoString = argIterator.next();
QStringList fileInfo = fileInfoString.split("=");
const QString &tag = fileInfo.at(0);
const QString &fileName = fileInfo.at(1);
m_page.m_allowedFiles[tag] = fileName;
}
}

connect(m_page.mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), SLOT(inject()));
connect(&m_page, SIGNAL(loadFinished(bool)), this, SLOT(finish(bool)));
Expand Down Expand Up @@ -293,6 +318,17 @@ 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);");
}


void Phantom::setState(const QString &value)
{
m_state = value;
Expand Down