"loadJsFile()" -> "injectJs"

* According to Issue #32 (http://code.google.com/p/phantomjs/issues/detail?id=32) I added a "lookup logic" that searchs for the file following those steps:
*# Search for file at given path (relative to PWD or absolute - no difference)
*# Is file there? Inject it
*# Is file not there? Try looking for it in "scriptLookupDir"
*# Is file there? Inject it
*# Is file not there? abort
* "scriptLookupDir" is an extra property for WebPage, that, as by it's name, defines a place where to look to script to inject
* Script can alter the scriptLookupDir, if they want
* Updated "injectme.js" accordingly
1.2
Ivan De Marino 2011-06-07 23:22:41 +01:00
parent 5b6f2731c4
commit ce0577adff
4 changed files with 31 additions and 5 deletions

View File

@ -16,7 +16,7 @@ if ( typeof(phantom) !== "undefined" ) {
console.log("* Script will 'inject' itself in a page...");
page.open("about:blank", function(status) {
if ( status === "success" ) {
console.log(page.loadJsFile("injectme.js") ? "... done injecting itself!" : "... fail! Check the $PWD?!");
console.log(page.injectJs("injectme.js") ? "... done injecting itself!" : "... fail! Check the $PWD?!");
}
phantom.exit();
});

View File

@ -34,6 +34,8 @@
#include <QtGui>
#include <QtWebKit>
#include <QDir>
#include <QFileInfo>
#include <gifwriter.h>
#include "consts.h"
@ -230,6 +232,7 @@ QObject *Phantom::createWebPage()
WebPage *page = new WebPage(this);
page->applySettings(m_defaultPageSettings);
page->setNetworkAccessManager(m_netAccessMan);
page->setScriptLookupDir(QFileInfo(m_scriptFile).dir().absolutePath());
return page;
}

View File

@ -160,6 +160,17 @@ void WebPage::setContent(const QString &content)
m_mainFrame->setHtml(content);
}
QString WebPage::scriptLookupDir() const
{
return m_scriptLookupDir;
}
void WebPage::setScriptLookupDir(const QString &dirPath)
{
m_scriptLookupDir = dirPath;
}
void WebPage::applySettings(const QVariantMap &def)
{
QWebSettings *opt = m_webPage->settings();
@ -456,10 +467,18 @@ void WebPage::click(const QString &selector) {
#endif
bool WebPage::loadJsFile(const QString &jsFilePath) {
if ( !jsFilePath.isEmpty()) {
bool WebPage::injectJs(const QString &jsFilePath) {
// Don't do anything if an empty string is passed
if (!jsFilePath.isEmpty()) {
QFile jsFile;
// Is file in the PWD?
jsFile.setFileName(jsFilePath);
if (!jsFile.exists()) {
// File is not in the PWD. Is it in the lookup directory?
jsFile.setFileName( m_scriptLookupDir + '/' + jsFilePath );
}
if ( jsFile.open(QFile::ReadOnly) ) {
// Execute JS code in the context of the document
m_mainFrame->evaluateJavaScript( QString::fromUtf8(jsFile.readAll()) );

View File

@ -42,6 +42,7 @@ class WebPage: public QObject
{
Q_OBJECT
Q_PROPERTY(QString content READ content WRITE setContent)
Q_PROPERTY(QString scriptLookupDir READ scriptLookupDir WRITE setScriptLookupDir)
Q_PROPERTY(QVariantMap viewportSize READ viewportSize WRITE setViewportSize)
Q_PROPERTY(QVariantMap paperSize READ paperSize WRITE setPaperSize)
Q_PROPERTY(QVariantMap clipRect READ clipRect WRITE setClipRect)
@ -55,6 +56,9 @@ public:
QString content() const;
void setContent(const QString &content);
QString scriptLookupDir() const;
void setScriptLookupDir(const QString &dirPath);
void setViewportSize(const QVariantMap &size);
QVariantMap viewportSize() const;
@ -64,12 +68,11 @@ public:
void setPaperSize(const QVariantMap &size);
QVariantMap paperSize() const;
public slots:
void openUrl(const QString &address, const QVariant &op, const QVariantMap &settings);
QVariant evaluate(const QString &code);
bool render(const QString &fileName);
bool loadJsFile(const QString &jsFilePath);
bool injectJs(const QString &jsFilePath);
// moc does not understand QT_VERSION_CHECK and hence the encoded hex
#if QT_VERSION >= 0x040600
@ -92,6 +95,7 @@ private:
QWebFrame *m_mainFrame;
QRect m_clipRect;
QVariantMap m_paperSize; // For PDF output via render()
QString m_scriptLookupDir;
QImage renderImage();
bool renderPdf(const QString &fileName);