phantomjs/src/webpage.h

551 lines
20 KiB
C
Raw Normal View History

2011-04-09 21:34:04 +04:00
/*
This file is part of the PhantomJS project from Ofi Labs.
Copyright (C) 2011 Ariya Hidayat <ariya.hidayat@gmail.com>
Copyright (C) 2011 Ivan De Marino <ivan.de.marino@gmail.com>
2011-04-09 21:34:04 +04:00
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2011-04-09 22:49:22 +04:00
#ifndef WEBPAGE_H
#define WEBPAGE_H
#include <QMap>
#include <QVariantMap>
#include <QtWebKitWidgets/QWebPage>
#include <QtWebKitWidgets/QWebFrame>
#include "cookiejar.h"
class Config;
class CustomPage;
class WebpageCallbacks;
class NetworkAccessManager;
class QWebInspector;
class Phantom;
class WebPage : public QObject
{
Q_OBJECT
Q_PROPERTY(QString title READ title)
Q_PROPERTY(QString frameTitle READ frameTitle)
Q_PROPERTY(QString content READ content WRITE setContent)
Q_PROPERTY(QString frameContent READ frameContent WRITE setFrameContent)
Q_PROPERTY(QString url READ url)
Q_PROPERTY(QString frameUrl READ frameUrl)
Q_PROPERTY(bool loading READ loading)
Q_PROPERTY(int loadingProgress READ loadingProgress)
Q_PROPERTY(bool canGoBack READ canGoBack)
Q_PROPERTY(bool canGoForward READ canGoForward)
Q_PROPERTY(QString plainText READ plainText)
Q_PROPERTY(QString framePlainText READ framePlainText)
2011-06-18 07:17:58 +04:00
Q_PROPERTY(QString libraryPath READ libraryPath WRITE setLibraryPath)
Q_PROPERTY(QString offlineStoragePath READ offlineStoragePath)
Q_PROPERTY(int offlineStorageQuota READ offlineStorageQuota)
Q_PROPERTY(QVariantMap viewportSize READ viewportSize WRITE setViewportSize)
Q_PROPERTY(QVariantMap paperSize READ paperSize WRITE setPaperSize)
Q_PROPERTY(QVariantMap clipRect READ clipRect WRITE setClipRect)
Q_PROPERTY(QVariantMap scrollPosition READ scrollPosition WRITE setScrollPosition)
Q_PROPERTY(bool navigationLocked READ navigationLocked WRITE setNavigationLocked)
Q_PROPERTY(QVariantMap customHeaders READ customHeaders WRITE setCustomHeaders)
Q_PROPERTY(qreal zoomFactor READ zoomFactor WRITE setZoomFactor)
Q_PROPERTY(QVariantList cookies READ cookies WRITE setCookies)
Q_PROPERTY(QString windowName READ windowName)
Q_PROPERTY(QObjectList pages READ pages)
Q_PROPERTY(QStringList pagesWindowName READ pagesWindowName)
Q_PROPERTY(bool ownsPages READ ownsPages WRITE setOwnsPages)
Q_PROPERTY(QStringList framesName READ framesName)
Q_PROPERTY(QString frameName READ frameName)
Q_PROPERTY(int framesCount READ framesCount)
Q_PROPERTY(QString focusedFrameName READ focusedFrameName)
Q_PROPERTY(QObject *cookieJar READ cookieJar WRITE setCookieJarFromQObject)
Q_PROPERTY(QVariantList cookies READ cookies WRITE setCookies)
public:
WebPage(QObject *parent, const QUrl &baseUrl = QUrl());
virtual ~WebPage();
QWebFrame *mainFrame();
QString content() const;
QString frameContent() const;
void setContent(const QString &content);
void setFrameContent(const QString &content);
QString title() const;
QString frameTitle() const;
QString url() const;
QString frameUrl() const;
bool loading() const;
int loadingProgress() const;
QString plainText() const;
QString framePlainText() const;
2011-06-18 07:17:58 +04:00
QString libraryPath() const;
void setLibraryPath(const QString &dirPath);
QString offlineStoragePath() const;
int offlineStorageQuota() const;
void setViewportSize(const QVariantMap &size);
QVariantMap viewportSize() const;
void setClipRect(const QVariantMap &size);
QVariantMap clipRect() const;
void setScrollPosition(const QVariantMap &size);
QVariantMap scrollPosition() const;
void setPaperSize(const QVariantMap &size);
QVariantMap paperSize() const;
void setNavigationLocked(bool lock);
bool navigationLocked();
void setCustomHeaders(const QVariantMap &headers);
QVariantMap customHeaders() const;
void showInspector(const int remotePort = -1);
QString footer(int page, int numPages);
qreal footerHeight() const;
QString header(int page, int numPages);
qreal headerHeight() const;
void setZoomFactor(qreal zoom);
qreal zoomFactor() const;
/**
* Value of <code>"window.name"</code> within the main page frame.
*
* It's just a convenience method for
* <code>"page.evaluate('return window.name;')"</code>
*
* @brief windowName
* @return Returns the value of <code>'window.name'</code>
* within the current frame
*/
QString windowName() const;
/**
* Returns a list of (Child) Pages that this page has currently open.
* A page opens chilp pages when using <code>"window.open()"</code>.
* If a child page spontaneously closes
* (i.e. a call to <code>"window.close()"<code>) or it's manually closed
* (i.e. "page.pages[i].release()"), the page is automatically removed by
* this array.
*
* NOTE: The ownership of this array is held by the Page: it's not adviced
* to have a "long running reference" to this array, as it might change.
* NOTE: If "ownsPages()" is "false", the page will create pages but not
* hold any ownership to it. Resource management is than left to the user.
*
* @brief pages
* @return List (JS Array) containing the Pages that this page
* has currently open.
*/
QObjectList pages() const;
/**
* Returns a list of (Child) Pages <code>"window.name"</code>.
*
* NOTE: When a page is opened with <code>"window.open"</code>, a window
* <code>"name"</code> might be provided as second parameter.
* This provides a useful list of those.
* NOTE: If "ownsPages()" is "false", the page will create pages but not
* hold any ownership of it. Resource management is than left to the user.
*
* @brief pagesWindowName
* @return List (JS Array) containing the <code>'window.name'</code>(s) of
* Pages that this page has currently open.
*/
QStringList pagesWindowName() const;
/**
* Returns "true" if it owns the pages it creates (and keeps them in "pages[]").
* Default value is "true". Can be changed using {@link setOwnsPages()}.
*
* @brief ownsPages()
* @return "true" if it owns the pages it creates in "pages[]", "false" otherwise.
*/
bool ownsPages() const;
/**
* Set if, from now on, it should own the pages it creates in "pages[]".
* Default value is "true".
*
* NOTE: When switching from "false" to "true", only the pages created
* from that point on will be owned. It's NOT retroactive.
*
* @brief setOwnsPages
* @param owns "true" to make it own the pages it creates in "pages[]", "false" otherwise.
*/
void setOwnsPages(const bool owns);
/**
* Returns the number of Child Frames inside the Current Frame.
* NOTE: The Current Frame changes when focus moves (via API or JS) to a specific child frame.
*
* @brief framesCount
* @return Number of Frames inside the Current Frame
*/
int framesCount() const;
/**
* Returns a list of (Child) Frames name.
* NOTE: The Current Frame changes when focus moves (via API or JS) to a specific child frame.
*
* @brief framesName
* @return List (JS Array) containing the names of the Child Frames inside the Current Frame (if any)
*/
QStringList framesName() const;
/**
* Returns the name of the (Current) Frame
*
* @brief frameName
* @return Name of the Current Frame
*/
QString frameName() const;
/**
* Returns the currently focused Frame's name.
*
* @brief focusedFrameName
* @return Frame
*/
QString focusedFrameName() const;
public slots:
void openUrl(const QString &address, const QVariant &op, const QVariantMap &settings);
void release();
void close();
QVariant evaluateJavaScript(const QString &code);
bool render(const QString &fileName, const QVariantMap &map = QVariantMap());
/**
* Render the page as base-64 encoded string.
* Default image format is "png".
*
* To choose a different format, pass a string with it's name.
* Available formats are the one supported by Qt QImageWriter class:
* @link http://qt-project.org/doc/qt-4.8/qimagewriter.html#supportedImageFormats.
*
* @brief renderBase64
* @param format String containing one of the supported types
* @return Rendering base-64 encoded of the page if the given format is supported, otherwise an empty string
*/
QString renderBase64(const QByteArray &format = "png");
bool injectJs(const QString &jsFilePath);
void _appendScriptElement(const QString &scriptUrl);
QObject *_getGenericCallback();
QObject *_getFilePickerCallback();
QObject *_getJsConfirmCallback();
QObject *_getJsPromptCallback();
QObject *_getJsInterruptCallback();
void _uploadFile(const QString &selector, const QStringList &fileNames);
void sendEvent(const QString &type, const QVariant &arg1 = QVariant(), const QVariant &arg2 = QVariant(), const QString &mouseButton = QString(), const QVariant &modifierArg = QVariant());
void setContent(const QString &content, const QString &baseUrl);
/**
* Returns a Child Page that matches the given <code>"window.name"</code>.
* This utility method is faster than accessing the
* <code>"windowName"</code> property of every <code>"page.pages"</code>
* and try to match.
*
* @brief getPage
* @param windowName
* @return Returns the page that matches <code>'window.name'</code>,
* or NULL if none is found
*/
QObject *getPage(const QString &windowName) const;
/**
* Returns the number of Child Frames inside the Current Frame.
* NOTE: The Current Frame changes when focus moves (via API or JS) to a specific child frame.
*
* @deprecated
* @brief childFramesCount
* @return Number of Frames inside the Current Frame
*/
int childFramesCount() const;
/**
* Returns a list of Child Frames name.
* NOTE: The Current Frame changes when focus moves (via API or JS) to a specific child frame.
*
* @deprecated
* @brief childFramesName
* @return List (JS Array) containing the names of the Child Frames inside the Current Frame (if any)
*/
QStringList childFramesName() const;
/**
* Switches focus from the Current Frame to a Child Frame, identified by it's name.
*
* @brief switchToFrame
* @param frameName Name of the Child frame
* @return "true" if the frame was found, "false" otherwise
*/
bool switchToFrame(const QString &frameName);
/**
* Switches focus from the Current Frame to a Child Frame, identified by it's name.
*
* @deprecated
* @brief switchToChildFrame
* @param frameName Name of the Child frame
* @return "true" if the frame was found, "false" otherwise
*/
bool switchToChildFrame(const QString &frameName);
/**
* Switches focus from the Current Frame to a Child Frame, identified by it positional order.
*
* @brief switchToFrame
* @param framePosition Position of the Frame inside the Child Frames array (i.e. "window.frames[i]")
* @return "true" if the frame was found, "false" otherwise
*/
bool switchToFrame(const int framePosition);
/**
* Switches focus from the Current Frame to a Child Frame, identified by it positional order.
*
* @deprecated
* @brief switchToChildFrame
* @param framePosition Position of the Frame inside the Child Frames array (i.e. "window.frames[i]")
* @return "true" if the frame was found, "false" otherwise
*/
bool switchToChildFrame(const int framePosition);
/**
* Switches focus to the Main Frame within this Page.
*
* @brief switchToMainFrame
*/
void switchToMainFrame();
/**
* Switches focus to the Parent Frame of the Current Frame (if it exists).
*
* @brief switchToParentFrame
* @return "true" if the Current Frame is not a Main Frame, "false" otherwise (i.e. there is no parent frame to switch to)
*/
bool switchToParentFrame();
/**
* Switches to the currently focused frame, as per QWebPage. This is the frame whose
* window element was last focus()ed, and is currently the target of key events.
*
* @brief switchToFocusedFrame
*/
void switchToFocusedFrame();
/**
* Returns the name of the Current Frame (if it has one)
*
* @deprecated
* @brief currentFrameName
* @return Name of the Current Frame
*/
QString currentFrameName() const;
/**
* Allows to set cookie jar for this page.
*/
void setCookieJar(CookieJar *cookieJar);
/**
* Allows to set cookie jar in through QtWebKit Bridge
*/
void setCookieJarFromQObject(QObject *cookieJar);
/**
* Returns the CookieJar object
*/
CookieJar *cookieJar();
/**
* Allows to set cookies by this Page, at the current URL.
* This means that loading new URLs, causes the cookies to change dynamically
* as in a normal desktop browser.
*
* Cookies are expected in the format:
* <pre>
* {
* "name" : "cookie name (string)",
* "value" : "cookie value (string)",
* "domain" : "cookie domain (string)",
* "path" : "cookie path (string, optional)",
* "httponly" : "http only cookie (boolean, optional)",
* "secure" : "secure cookie (boolean, optional)",
* "expires" : "expiration date (string, GMT format, optional)"
* }
* </pre>
* @brief setCookies
* @param cookies Expects a QList of QVariantMaps
Redesign the Cookies API (part 2) Addresses [Issue #761](http://code.google.com/p/phantomjs/issues/detail?id=761). This is a combination of 5 commits. 1. Date in Cookie can be set via "seconds since epoch" as well. * In addition to the current string format, we can now set cookies via integer of msec since epoch * Expiration date can be set via "expires" or "expiry" option ("expires" has priority) * Returned cookie will contain "expires" as string and "expiry" as msec since epoch I believe this can simplify code that uses cookies and it doesn't change the functionality. 2. Applying the "--debug" command line options as early as possible. 3. Fixing bug and behaviour in the CookieJar * It's not possible to set a cookie without a domain: will default to the domain of the page it's set on * "page.clearCookies()" was broken * "cookiejar.deleteCookie("name", url)" reimplemented because deleting via "expiration" doesn't work 4. Improving (and more fixing) in the CookieJar * Purging Session or Expired Cookies now works * Added boolean return values to inform if the requested cookie operation succeeded * Timestamps for "expiry/expires" in Milliseconds, as JS does by default * Improved detection that a cookie has been accepted or rejected by the cookiejar NOTE: Unfortunately, the Qt provided QNetworkCookieJar is a very limited and not extremely well designed solution. It doesn't provide any "nice and clean" CRUD API, and you are only left with a SET/GET to deal with. Mechanism to understand when and when not a cookie is added are hidden, and require extra work in the sub-class (our CookieJar) to fill the gap. 5. Methods on the "phantom" object to manipulate the CookieJar. * phantom.cookies (array of JSON/Cookies) * phantom.cookiesEnabled (boolean) * phantom.addCookie * phantom.deleteCookie * phantom.clearCookies Those methods operate on the CookieJar directly and have no URL restriction. In other words, if page P1 can see set of cookies C1, and page P2 can see set of cookies C2, "phantom.cookies" can see (i.e. operate upon) both C1 and C2.
2012-09-09 15:24:15 +04:00
* @return Boolean "true" if at least 1 cookie was set
*/
Redesign the Cookies API (part 2) Addresses [Issue #761](http://code.google.com/p/phantomjs/issues/detail?id=761). This is a combination of 5 commits. 1. Date in Cookie can be set via "seconds since epoch" as well. * In addition to the current string format, we can now set cookies via integer of msec since epoch * Expiration date can be set via "expires" or "expiry" option ("expires" has priority) * Returned cookie will contain "expires" as string and "expiry" as msec since epoch I believe this can simplify code that uses cookies and it doesn't change the functionality. 2. Applying the "--debug" command line options as early as possible. 3. Fixing bug and behaviour in the CookieJar * It's not possible to set a cookie without a domain: will default to the domain of the page it's set on * "page.clearCookies()" was broken * "cookiejar.deleteCookie("name", url)" reimplemented because deleting via "expiration" doesn't work 4. Improving (and more fixing) in the CookieJar * Purging Session or Expired Cookies now works * Added boolean return values to inform if the requested cookie operation succeeded * Timestamps for "expiry/expires" in Milliseconds, as JS does by default * Improved detection that a cookie has been accepted or rejected by the cookiejar NOTE: Unfortunately, the Qt provided QNetworkCookieJar is a very limited and not extremely well designed solution. It doesn't provide any "nice and clean" CRUD API, and you are only left with a SET/GET to deal with. Mechanism to understand when and when not a cookie is added are hidden, and require extra work in the sub-class (our CookieJar) to fill the gap. 5. Methods on the "phantom" object to manipulate the CookieJar. * phantom.cookies (array of JSON/Cookies) * phantom.cookiesEnabled (boolean) * phantom.addCookie * phantom.deleteCookie * phantom.clearCookies Those methods operate on the CookieJar directly and have no URL restriction. In other words, if page P1 can see set of cookies C1, and page P2 can see set of cookies C2, "phantom.cookies" can see (i.e. operate upon) both C1 and C2.
2012-09-09 15:24:15 +04:00
bool setCookies(const QVariantList &cookies);
/**
* Cookies visible by this Page, at the current URL.
*
* @see WebPage::setCookies for details on the format
* @brief cookies
* @return QList of QVariantMap cookies visible to this Page, at the current URL.
*/
QVariantList cookies() const;
/**
* Add a Cookie in QVariantMap format
* @see WebPage::setCookies for details on the format
* @brief addCookie
* @param cookie Cookie in QVariantMap format
Redesign the Cookies API (part 2) Addresses [Issue #761](http://code.google.com/p/phantomjs/issues/detail?id=761). This is a combination of 5 commits. 1. Date in Cookie can be set via "seconds since epoch" as well. * In addition to the current string format, we can now set cookies via integer of msec since epoch * Expiration date can be set via "expires" or "expiry" option ("expires" has priority) * Returned cookie will contain "expires" as string and "expiry" as msec since epoch I believe this can simplify code that uses cookies and it doesn't change the functionality. 2. Applying the "--debug" command line options as early as possible. 3. Fixing bug and behaviour in the CookieJar * It's not possible to set a cookie without a domain: will default to the domain of the page it's set on * "page.clearCookies()" was broken * "cookiejar.deleteCookie("name", url)" reimplemented because deleting via "expiration" doesn't work 4. Improving (and more fixing) in the CookieJar * Purging Session or Expired Cookies now works * Added boolean return values to inform if the requested cookie operation succeeded * Timestamps for "expiry/expires" in Milliseconds, as JS does by default * Improved detection that a cookie has been accepted or rejected by the cookiejar NOTE: Unfortunately, the Qt provided QNetworkCookieJar is a very limited and not extremely well designed solution. It doesn't provide any "nice and clean" CRUD API, and you are only left with a SET/GET to deal with. Mechanism to understand when and when not a cookie is added are hidden, and require extra work in the sub-class (our CookieJar) to fill the gap. 5. Methods on the "phantom" object to manipulate the CookieJar. * phantom.cookies (array of JSON/Cookies) * phantom.cookiesEnabled (boolean) * phantom.addCookie * phantom.deleteCookie * phantom.clearCookies Those methods operate on the CookieJar directly and have no URL restriction. In other words, if page P1 can see set of cookies C1, and page P2 can see set of cookies C2, "phantom.cookies" can see (i.e. operate upon) both C1 and C2.
2012-09-09 15:24:15 +04:00
* @return Boolean "true" if cookie was added
*/
Redesign the Cookies API (part 2) Addresses [Issue #761](http://code.google.com/p/phantomjs/issues/detail?id=761). This is a combination of 5 commits. 1. Date in Cookie can be set via "seconds since epoch" as well. * In addition to the current string format, we can now set cookies via integer of msec since epoch * Expiration date can be set via "expires" or "expiry" option ("expires" has priority) * Returned cookie will contain "expires" as string and "expiry" as msec since epoch I believe this can simplify code that uses cookies and it doesn't change the functionality. 2. Applying the "--debug" command line options as early as possible. 3. Fixing bug and behaviour in the CookieJar * It's not possible to set a cookie without a domain: will default to the domain of the page it's set on * "page.clearCookies()" was broken * "cookiejar.deleteCookie("name", url)" reimplemented because deleting via "expiration" doesn't work 4. Improving (and more fixing) in the CookieJar * Purging Session or Expired Cookies now works * Added boolean return values to inform if the requested cookie operation succeeded * Timestamps for "expiry/expires" in Milliseconds, as JS does by default * Improved detection that a cookie has been accepted or rejected by the cookiejar NOTE: Unfortunately, the Qt provided QNetworkCookieJar is a very limited and not extremely well designed solution. It doesn't provide any "nice and clean" CRUD API, and you are only left with a SET/GET to deal with. Mechanism to understand when and when not a cookie is added are hidden, and require extra work in the sub-class (our CookieJar) to fill the gap. 5. Methods on the "phantom" object to manipulate the CookieJar. * phantom.cookies (array of JSON/Cookies) * phantom.cookiesEnabled (boolean) * phantom.addCookie * phantom.deleteCookie * phantom.clearCookies Those methods operate on the CookieJar directly and have no URL restriction. In other words, if page P1 can see set of cookies C1, and page P2 can see set of cookies C2, "phantom.cookies" can see (i.e. operate upon) both C1 and C2.
2012-09-09 15:24:15 +04:00
bool addCookie(const QVariantMap &cookie);
/**
* Delete cookie by name from the ones visible by this Page, at the current URL
* @brief deleteCookie
* @param cookieName Name of the Cookie to delete
Redesign the Cookies API (part 2) Addresses [Issue #761](http://code.google.com/p/phantomjs/issues/detail?id=761). This is a combination of 5 commits. 1. Date in Cookie can be set via "seconds since epoch" as well. * In addition to the current string format, we can now set cookies via integer of msec since epoch * Expiration date can be set via "expires" or "expiry" option ("expires" has priority) * Returned cookie will contain "expires" as string and "expiry" as msec since epoch I believe this can simplify code that uses cookies and it doesn't change the functionality. 2. Applying the "--debug" command line options as early as possible. 3. Fixing bug and behaviour in the CookieJar * It's not possible to set a cookie without a domain: will default to the domain of the page it's set on * "page.clearCookies()" was broken * "cookiejar.deleteCookie("name", url)" reimplemented because deleting via "expiration" doesn't work 4. Improving (and more fixing) in the CookieJar * Purging Session or Expired Cookies now works * Added boolean return values to inform if the requested cookie operation succeeded * Timestamps for "expiry/expires" in Milliseconds, as JS does by default * Improved detection that a cookie has been accepted or rejected by the cookiejar NOTE: Unfortunately, the Qt provided QNetworkCookieJar is a very limited and not extremely well designed solution. It doesn't provide any "nice and clean" CRUD API, and you are only left with a SET/GET to deal with. Mechanism to understand when and when not a cookie is added are hidden, and require extra work in the sub-class (our CookieJar) to fill the gap. 5. Methods on the "phantom" object to manipulate the CookieJar. * phantom.cookies (array of JSON/Cookies) * phantom.cookiesEnabled (boolean) * phantom.addCookie * phantom.deleteCookie * phantom.clearCookies Those methods operate on the CookieJar directly and have no URL restriction. In other words, if page P1 can see set of cookies C1, and page P2 can see set of cookies C2, "phantom.cookies" can see (i.e. operate upon) both C1 and C2.
2012-09-09 15:24:15 +04:00
* @return Boolean "true" if cookie was deleted
*/
Redesign the Cookies API (part 2) Addresses [Issue #761](http://code.google.com/p/phantomjs/issues/detail?id=761). This is a combination of 5 commits. 1. Date in Cookie can be set via "seconds since epoch" as well. * In addition to the current string format, we can now set cookies via integer of msec since epoch * Expiration date can be set via "expires" or "expiry" option ("expires" has priority) * Returned cookie will contain "expires" as string and "expiry" as msec since epoch I believe this can simplify code that uses cookies and it doesn't change the functionality. 2. Applying the "--debug" command line options as early as possible. 3. Fixing bug and behaviour in the CookieJar * It's not possible to set a cookie without a domain: will default to the domain of the page it's set on * "page.clearCookies()" was broken * "cookiejar.deleteCookie("name", url)" reimplemented because deleting via "expiration" doesn't work 4. Improving (and more fixing) in the CookieJar * Purging Session or Expired Cookies now works * Added boolean return values to inform if the requested cookie operation succeeded * Timestamps for "expiry/expires" in Milliseconds, as JS does by default * Improved detection that a cookie has been accepted or rejected by the cookiejar NOTE: Unfortunately, the Qt provided QNetworkCookieJar is a very limited and not extremely well designed solution. It doesn't provide any "nice and clean" CRUD API, and you are only left with a SET/GET to deal with. Mechanism to understand when and when not a cookie is added are hidden, and require extra work in the sub-class (our CookieJar) to fill the gap. 5. Methods on the "phantom" object to manipulate the CookieJar. * phantom.cookies (array of JSON/Cookies) * phantom.cookiesEnabled (boolean) * phantom.addCookie * phantom.deleteCookie * phantom.clearCookies Those methods operate on the CookieJar directly and have no URL restriction. In other words, if page P1 can see set of cookies C1, and page P2 can see set of cookies C2, "phantom.cookies" can see (i.e. operate upon) both C1 and C2.
2012-09-09 15:24:15 +04:00
bool deleteCookie(const QString &cookieName);
/**
* Delete All Cookies visible by this Page, at the current URL
* @brief clearCookies
Redesign the Cookies API (part 2) Addresses [Issue #761](http://code.google.com/p/phantomjs/issues/detail?id=761). This is a combination of 5 commits. 1. Date in Cookie can be set via "seconds since epoch" as well. * In addition to the current string format, we can now set cookies via integer of msec since epoch * Expiration date can be set via "expires" or "expiry" option ("expires" has priority) * Returned cookie will contain "expires" as string and "expiry" as msec since epoch I believe this can simplify code that uses cookies and it doesn't change the functionality. 2. Applying the "--debug" command line options as early as possible. 3. Fixing bug and behaviour in the CookieJar * It's not possible to set a cookie without a domain: will default to the domain of the page it's set on * "page.clearCookies()" was broken * "cookiejar.deleteCookie("name", url)" reimplemented because deleting via "expiration" doesn't work 4. Improving (and more fixing) in the CookieJar * Purging Session or Expired Cookies now works * Added boolean return values to inform if the requested cookie operation succeeded * Timestamps for "expiry/expires" in Milliseconds, as JS does by default * Improved detection that a cookie has been accepted or rejected by the cookiejar NOTE: Unfortunately, the Qt provided QNetworkCookieJar is a very limited and not extremely well designed solution. It doesn't provide any "nice and clean" CRUD API, and you are only left with a SET/GET to deal with. Mechanism to understand when and when not a cookie is added are hidden, and require extra work in the sub-class (our CookieJar) to fill the gap. 5. Methods on the "phantom" object to manipulate the CookieJar. * phantom.cookies (array of JSON/Cookies) * phantom.cookiesEnabled (boolean) * phantom.addCookie * phantom.deleteCookie * phantom.clearCookies Those methods operate on the CookieJar directly and have no URL restriction. In other words, if page P1 can see set of cookies C1, and page P2 can see set of cookies C2, "phantom.cookies" can see (i.e. operate upon) both C1 and C2.
2012-09-09 15:24:15 +04:00
* @return Boolean "true" if cookies were deleted
*/
Redesign the Cookies API (part 2) Addresses [Issue #761](http://code.google.com/p/phantomjs/issues/detail?id=761). This is a combination of 5 commits. 1. Date in Cookie can be set via "seconds since epoch" as well. * In addition to the current string format, we can now set cookies via integer of msec since epoch * Expiration date can be set via "expires" or "expiry" option ("expires" has priority) * Returned cookie will contain "expires" as string and "expiry" as msec since epoch I believe this can simplify code that uses cookies and it doesn't change the functionality. 2. Applying the "--debug" command line options as early as possible. 3. Fixing bug and behaviour in the CookieJar * It's not possible to set a cookie without a domain: will default to the domain of the page it's set on * "page.clearCookies()" was broken * "cookiejar.deleteCookie("name", url)" reimplemented because deleting via "expiration" doesn't work 4. Improving (and more fixing) in the CookieJar * Purging Session or Expired Cookies now works * Added boolean return values to inform if the requested cookie operation succeeded * Timestamps for "expiry/expires" in Milliseconds, as JS does by default * Improved detection that a cookie has been accepted or rejected by the cookiejar NOTE: Unfortunately, the Qt provided QNetworkCookieJar is a very limited and not extremely well designed solution. It doesn't provide any "nice and clean" CRUD API, and you are only left with a SET/GET to deal with. Mechanism to understand when and when not a cookie is added are hidden, and require extra work in the sub-class (our CookieJar) to fill the gap. 5. Methods on the "phantom" object to manipulate the CookieJar. * phantom.cookies (array of JSON/Cookies) * phantom.cookiesEnabled (boolean) * phantom.addCookie * phantom.deleteCookie * phantom.clearCookies Those methods operate on the CookieJar directly and have no URL restriction. In other words, if page P1 can see set of cookies C1, and page P2 can see set of cookies C2, "phantom.cookies" can see (i.e. operate upon) both C1 and C2.
2012-09-09 15:24:15 +04:00
bool clearCookies();
/**
* Checks if this Page can go back in the Navigation History
* @brief canGoBack
* @return "true" if it can, "false" otherwise
*/
bool canGoBack();
/**
* Goes back in the Navigation History
* @brief goBack
* @return "true" if it does go back in the Navigation History, "false" otherwise
*/
bool goBack();
/**
* Checks if this Page can go forward in the Navigation History (i.e. next URL)
* @brief canGoForward
* @return "true" if it can, "false" otherwise
*/
bool canGoForward();
/**
* Goes forward in the Navigation History
* @brief goForward
* @return "true" if it does go forward in the Navigation History, "false" otherwise
*/
bool goForward();
/**
* Go to the page identified by its relative location to the current page.
* For example '-1' for the previous page or 1 for the next page.
*
* Modelled after JavaScript "window.go(num)" method:
* {@see https://developer.mozilla.org/en-US/docs/DOM/window.history#Syntax}.
* @brief go
* @param historyRelativeIndex
* @return "true" if it does go forward/backgward in the Navigation History, "false" otherwise
*/
bool go(int historyRelativeIndex);
/**
* Reload current page
* @brief reload
*/
void reload();
/**
* Stop loading page (if the page is loading)
*
* NOTE: This method does nothing when page is not actually loading.
* It's effect can be applied in that very short window of time between
* "onLoadStarted" and "onLoadFinished".
*
* @brief stop
*/
void stop();
void stopJavaScript();
void clearMemoryCache();
signals:
void initialized();
void loadStarted();
void loadFinished(const QString &status);
void javaScriptAlertSent(const QString &msg);
void javaScriptConsoleMessageSent(const QString &message);
void javaScriptErrorSent(const QString &msg, int lineNumber, const QString &sourceID, const QString &stack);
void resourceRequested(const QVariant &requestData, QObject *request);
void resourceReceived(const QVariant &resource);
void resourceError(const QVariant &errorData);
void resourceTimeout(const QVariant &errorData);
void urlChanged(const QUrl &url);
void navigationRequested(const QUrl &url, const QString &navigationType, bool navigationLocked, bool isMainFrame);
void rawPageCreated(QObject *page);
void closing(QObject *page);
void repaintRequested(const int x, const int y, const int width, const int height);
private slots:
void finish(bool ok);
void setupFrame(QWebFrame *frame = NULL);
void updateLoadingProgress(int progress);
void handleRepaintRequested(const QRect &dirtyRect);
A REPL for PhantomJS This covers [Issue 252](http://code.google.com/p/phantomjs/issues/detail?id=252) The commit is composed of 12 squashed commits: commit efdc6ba4f143c30a690fd97d92d80fa412e79999 Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Mon Feb 27 00:19:36 2012 +0000 Pretty-pringing and Completion Caching done! * This completes pretty-printing for the result of evaluated * expressions in the REPL. * Also, now we cache the "possible completions", to speed things up * a bit (nothing fancy though). * Minor tweaks to the internal doc and the way we "mock" * pretty-printing for QObjects/REPLCompletanle * All tests passing :) commit 1f9ef690e112a535b431fca409b77bb9c09d1c70 Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Sun Feb 26 22:35:00 2012 +0000 Moving most of REPL shim JavaScritp code in a separate file. Way easier to work on. commit 02d460a16fee14e7096ae7d899c03902c5b8a9c6 Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Sat Feb 25 20:25:18 2012 +0000 Initialisation of the Completions is now done in a pure virtual. This means that every REPLCompletable object will ACTUALLY register completion strings, ONLY if we are running a REPL and that object is ACTUALLY created. Otherwise, why bother? Adding completions for all exposed REPLCompletable objects Also, fixed an issue with _getCompletions() commit 412c3778fb04aa1c7379f8e760afce702b0428dd Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Tue Feb 21 00:49:17 2012 +0000 Few more tweaks to the REPL: - Now 'phantom' is the first QObject with proper completion - No repetition in QObject completions - LVAL of any user expression is now correctly prettified and printed Major things left to do: - Cache completions (using QCache?) - Add completions for the other QObject - When the LVAL of a user expression is a QObject, print what's expected, not the QObject "real" structure commit 46f04713c8165d898055e15478bb31403f8c93f1 Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Tue Feb 7 10:13:23 2012 -0800 Pretty-print expressions result Still not done though: there are issues with the NON-Native JS objects. commit 98b2fe67651dc750b62c6fa9cf1d80317fd9ae06 Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Fri Feb 3 00:22:52 2012 -0800 Introducing REPLCompletable. This class should be inherited by any JavaScript-exposed QObject, to ensure correct Auto-Completion. Correct auto-completion for QObjects. - Now even QObjects can correctly provide auto-completion, and avoid showing "not for users" methods - The strings used for the auto-completion are stored in a single Index: minimum memory footprint - Still, there is optimization that should be done (when "searching" for the right completion by prefix) - Completion for the objects not set up yet, but now it's just a trivial sequence of "addCompletion('bla')" in their constructors commit 9bd48618154b1530a37b41f4060440184e23253d Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Thu Feb 2 00:20:25 2012 -0800 Changing the way we import Linenoise. Will just import a specific commit, and update manually when needed. commit cfc9bae9fbdab13b01019b34b7cbd565e3153780 Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Sun Jan 29 23:22:26 2012 -0800 Made the REPL into a Singleton. With Auto-completion!. Reasons: 1) Needed a pointer to function (i.e. a static method) to be used with Linenoise to provide auto-completions 2) It makes more sense, as it's not like we are going to have 2 REPL running at the same time, are we? There are problems to address: - the enumeration in JS seems to return only the native interface of our objects - the function completions contain argument types of those functions - "private" methods are exposed commit c78bd32e17f8e0e4cc4a0066858de8cc81d33b97 Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Sun Jan 29 22:10:20 2012 -0800 Migrating from the original, now [unmantained Linenoise](https://github.com/antirez/linenoise) to the fairly active [tadmarshall fork](https://github.com/tadmarshall/linenoise). Also now the project is imported as a Git Submodule. Having migrated to the latest Linenoise (see prev. commit), now this _SHOULD_ work on Windows too. But, of course, this needs testing. :) commit 43713c5723d7c5ed446ba41ae8d6f8c9feba7f9b Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Tue Jan 24 23:17:06 2012 -0800 Now that the basics work, I'm adding support for REPL history. This is something almost everyone today is accustomed to. Also, now REPL history works! And I found some useful resources to solve pending TODOs. commit 31e5f88b044a5b4a823c67527ef8c245d2ac7863 Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Sun Jan 22 20:56:36 2012 -0800 Adding Linenoise Project (https://github.com/antirez/linenoise). For now is included as a drop-in set of files. Later on, if the Linenoise project has frequent updates, we might prefer to do it as a git-submodule. commit 4be9c15c65db4767e482fba0be13f8aab286d5f3 Author: Ivan De Marino <ivan.de.marino@gmail.com> Date: Thu Jan 5 15:31:13 2012 +0000 First simple REPL implementation. - Not complete - Still doesn't handle arrow keys (needed for history)
2012-01-05 19:31:13 +04:00
private:
QImage renderImage();
bool renderPdf(const QString &fileName);
void applySettings(const QVariantMap &defaultSettings);
QString userAgent() const;
/**
* Switches focus from the Current Frame to the Child Frame, identified by `frame`.
*
* @brief changeCurrentFrame
* @param frame The Child frame
*/
void changeCurrentFrame(QWebFrame * const frame);
QString filePicker(const QString &oldFile);
bool javaScriptConfirm(const QString &msg);
bool javaScriptPrompt(const QString &msg, const QString &defaultValue, QString *result);
void javascriptInterrupt();
private:
CustomPage *m_customWebPage;
NetworkAccessManager *m_networkAccessManager;
QWebFrame *m_mainFrame;
QWebFrame *m_currentFrame;
QRect m_clipRect;
QPoint m_scrollPosition;
QVariantMap m_paperSize; // For PDF output via render()
2011-06-18 07:17:58 +04:00
QString m_libraryPath;
QWebInspector* m_inspector;
WebpageCallbacks *m_callbacks;
bool m_navigationLocked;
QPoint m_mousePos;
bool m_ownsPages;
int m_loadingProgress;
bool m_shouldInterruptJs;
CookieJar *m_cookieJar;
friend class Phantom;
friend class CustomPage;
};
2011-04-09 22:49:22 +04:00
#endif // WEBPAGE_H