Permalink
Browse files

Moved Javascript code into a .js file that gets loaded into each page

  • Loading branch information...
1 parent e0c4f6f commit 54d7df3c4c540f13347a39ee8878e975a412f2e9 @jferris jferris committed Feb 26, 2011
Showing with 43 additions and 29 deletions.
  1. +1 −6 src/Attribute.cpp
  2. +1 −22 src/Find.cpp
  3. +9 −0 src/WebPage.cpp
  4. +4 −0 src/WebPage.h
  5. +21 −0 src/capybara.js
  6. +2 −1 src/webkit_server.pro
  7. +5 −0 src/webkit_server.qrc
View
@@ -9,12 +9,7 @@ void Attribute::receivedArgument(const char *argument) {
if (m_args.length() == 2) {
QString nodeIndex = m_args[0];
QString attributeName = m_args[1];
- QString javascript = QString("\
- (function () {\
- var node = window.__capybara_nodes[") + nodeIndex + "];\
- return node.getAttribute('" + attributeName + "');\
- })();\
- ";
+ QString javascript = QString("Capybara.attribute(" + nodeIndex + ", \"" + attributeName + "\")");
QVariant result = page()->mainFrame()->evaluateJavaScript(javascript);
QString attributeValue = result.toString();
emit finished(true, attributeValue);
View
@@ -8,28 +8,7 @@ Find::Find(WebPage *page, QObject *parent) : Command(page, parent) {
void Find::receivedArgument(const char *xpath) {
std::cout << "<< Running query: " << xpath << std::endl;
- QString javascript = QString("\
- (function () {\
- if (!window.__capybara_index) {\
- window.__capybara_index = 0;\
- window.__capybara_nodes = {};\
- }\
- var iterator = document.evaluate(\"") + xpath + "\",\
- document,\
- null,\
- XPathResult.ORDERED_NODE_ITERATOR_TYPE,\
- null);\
- var node;\
- var results = [];\
- while (node = iterator.iterateNext()) {\
- window.__capybara_index++;\
- window.__capybara_nodes[window.__capybara_index] = node;\
- results.push(window.__capybara_index);\
- }\
- return results;\
- })()\
- ";
-
+ QString javascript = QString("Capybara.find(\"") + xpath + "\")";
std::cout << "<< Javascript to execute:" << std::endl;
std::cout << javascript.toAscii().data() << std::endl;
View
@@ -1,6 +1,15 @@
#include "WebPage.h"
+#include <QResource>
WebPage::WebPage(QObject *parent) : QWebPage(parent) {
+ connect(mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),
+ this, SLOT(injectJavascriptHelpers()));
+ QResource javascript(":/capybara.js");
+ m_capybaraJavascript = QString((const char *) javascript.data());
+}
+
+void WebPage::injectJavascriptHelpers() {
+ mainFrame()->evaluateJavaScript(m_capybaraJavascript);
}
bool WebPage::shouldInterruptJavaScript() {
View
@@ -9,5 +9,9 @@ class WebPage : public QWebPage {
public slots:
bool shouldInterruptJavaScript();
+ void injectJavascriptHelpers();
+
+ private:
+ QString m_capybaraJavascript;
};
View
@@ -0,0 +1,21 @@
+Capybara = {
+ nextIndex: 0,
+ nodes: {},
+
+ find: function (xpath) {
+ var iterator = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
+ var node;
+ var results = [];
+ while (node = iterator.iterateNext()) {
+ this.nextIndex++;
+ this.nodes[this.nextIndex] = node;
+ results.push(this.nextIndex);
+ }
+ return results;
+ },
+
+ attribute: function (index, name) {
+ return this.nodes[index].getAttribute(name);
+ }
+};
+
View
@@ -3,6 +3,7 @@ TARGET = webkit_server
DESTDIR = .
HEADERS = WebPage.h Server.h Connection.h Command.h Visit.h Find.h Reset.h Attribute.h
SOURCES = main.cpp WebPage.cpp Server.cpp Connection.cpp Command.cpp Visit.cpp Find.cpp Reset.cpp Attribute.cpp
+RESOURCES = webkit_server.qrc
QT += network webkit
-CONFIG += console staticlib
+CONFIG += console
View
@@ -0,0 +1,5 @@
+<RCC>
+ <qresource prefix="/">
+ <file>capybara.js</file>
+ </qresource>
+</RCC>

0 comments on commit 54d7df3

Please sign in to comment.