Permalink
Browse files

fix - make click more type click on buttons that are removed and adde…

…d after ajax. Closes #50
  • Loading branch information...
1 parent 0dc3b92 commit 2b7c04841ca9d1cb1c3b86ce3d97e8dd36d8ac09 @martinsbalodis committed Sep 13, 2014
@@ -72,6 +72,7 @@
"scripts/ContentSelector.js",
"scripts/Selector.js",
"scripts/ElementQuery.js",
+ "scripts/UniqueElementList.js",
"scripts/Selector/SelectorElement.js",
"scripts/Selector/SelectorGroup.js",
"scripts/Selector/SelectorLink.js",
@@ -161,42 +161,42 @@ var SelectorElementClick = {
var delay = parseInt(this.delay) || 0;
var deferredResponse = $.Deferred();
- var foundElements = [];
+ var foundElements = new UniqueElementList();
var clickElements = this.getClickElements(parentElement);
-
- // @TODO refactor. create unique element list class
- var addedElements = {};
- var addElement = function(element) {
-
- var elementTxt = $(element).text().trim();
- if(elementTxt in addedElements) {
- return false;
- }
- else {
- addedElements[elementTxt] = true;
- foundElements.push($(element).clone(true)[0]);
- return true;
- }
- };
+ var doneClickingElements = new UniqueElementList();
// add elements that are available before clicking
var elements = this.getDataElements(parentElement);
- elements.forEach(addElement);
+ elements.forEach(foundElements.push.bind(foundElements));
// discard initial elements
if(this.discardInitialElements) {
- foundElements = [];
+ foundElements = new UniqueElementList();
}
- // initial click and wait
- if(clickElements.length) {
- this.triggerButtonClick(clickElements[0]);
+ // no elements to click at the beginning
+ if(clickElements.length === 0) {
+ deferredResponse.resolve(foundElements);
+ return;
}
+
+ // initial click and wait
+ var currentClickElement = clickElements[0];
+ this.triggerButtonClick(currentClickElement);
var nextElementSelection = (new Date()).getTime()+delay;
// infinitely scroll down and find all items
var interval = setInterval(function() {
+ // find those click elements that are not in the black list
+ var allClickElements = this.getClickElements(parentElement);
+ clickElements = [];
+ allClickElements.forEach(function(element) {
+ if(!doneClickingElements.isAdded(element)) {
+ clickElements.push(element);
+ }
+ });
+
// no elements to click
if(clickElements.length === 0) {
clearInterval(interval);
@@ -213,19 +213,20 @@ var SelectorElementClick = {
var elements = this.getDataElements(parentElement);
var addedAnElement = false;
elements.forEach(function(element) {
- var added = addElement(element);
+ var added = foundElements.push(element);
if(added) {
addedAnElement = true;
}
});
- // no new elements found
+ // no new elements found. Stop clicking this button
if(!addedAnElement) {
- clickElements.shift();
+ doneClickingElements.push(currentClickElement);
}
else {
// continue scrolling and add delay
- this.triggerButtonClick(clickElements[0]);
+ currentClickElement = clickElements[0];
+ this.triggerButtonClick(currentClickElement);
nextElementSelection = now+delay;
}
}.bind(this), 50);
@@ -0,0 +1,35 @@
+/**
+ * Only Elements with unique text will be added to this array
+ * @constructor
+ */
+UniqueElementList = function() {
+ this.addedElements = {};
+};
+
+UniqueElementList.prototype = new Array;
+
+UniqueElementList.prototype.push = function(element) {
+
+ if(this.isAdded(element)) {
+ return false;
+ }
+ else {
+ var elementTxt = this.getElementText(element);
+ this.addedElements[elementTxt] = true;
+ Array.prototype.push.call(this, $(element).clone(true)[0]);
+ return true;
+ }
+};
+
+UniqueElementList.prototype.getElementText = function(element) {
+
+ var elementTxt = $(element).text().trim();
+ return elementTxt;
+};
+
+UniqueElementList.prototype.isAdded = function(element) {
+
+ var elementTxt = this.getElementText(element);
+ var isAdded = elementTxt in this.addedElements;
+ return isAdded;
+};
@@ -21,6 +21,7 @@
<script src="../../extension/scripts/Selector.js"></script>
<script src="../../extension/scripts/SelectorList.js"></script>
<script src="../../extension/scripts/ElementQuery.js"></script>
+ <script src="../../extension/scripts/UniqueElementList.js"></script>
<script src="../../extension/scripts/Selector/SelectorLink.js"></script>
<script src="../../extension/scripts/Selector/SelectorPopupLink.js"></script>
<script src="../../extension/scripts/Selector/SelectorImage.js"></script>
@@ -32,6 +32,7 @@
<script type="text/javascript" src="../extension/scripts/ContentScript.js"></script>
<script type="text/javascript" src="../extension/scripts/BackgroundScript.js"></script>
<script type="text/javascript" src="../extension/scripts/ElementQuery.js"></script>
+ <script type="text/javascript" src="../extension/scripts/UniqueElementList.js"></script>
<script src="../extension/scripts/Selector/SelectorText.js"></script>
<script src="../extension/scripts/Selector/SelectorElement.js"></script>
<script src="../extension/scripts/Selector/SelectorLink.js"></script>
@@ -90,6 +91,7 @@
<script type="text/javascript" src="spec/Selector/SelectorElementClickSpec.js"></script>
<script type="text/javascript" src="spec/ChromePopupBrowserSpec.js"></script>
<script type="text/javascript" src="spec/ElementQuerySpec.js"></script>
+ <script type="text/javascript" src="spec/UniqueElementListSpec.js"></script>
<script type="text/javascript">
(function () {
@@ -354,12 +354,62 @@ describe("Click Element Selector", function () {
});
});
+ it("should click buttons that are added", function(){
+
+ $el.append($("<a>1</a><div>a</div>"));
+ var moreElements = ['b','c'];
+ var clickHandler = function() {
+ setTimeout(function() {
+ var next = moreElements.shift();
+ if(next) {
+ $el.append("<div>"+next+"</div>");
+ $el.find("a").remove();
+ $el.append($("<a>1</a>"));
+ $el.find("a").click(clickHandler);
+ }
+ }, 50);
+ };
+
+ $el.find("a").click(clickHandler);
+
+ var selector = new Selector({
+ id: 'div',
+ type: 'SelectorElementClick',
+ multiple: true,
+ clickElementSelector: "a",
+ selector: "div",
+ delay: 100,
+ clickType: 'clickMore'
+ });
+
+ var dataDeferred = selector.getData($el);
+
+ waitsFor(function() {
+ return dataDeferred.state() === 'resolved';
+ }, "wait for data extraction", 5000);
+
+ runs(function () {
+
+ dataDeferred.done(function(resultData) {
+ expect(resultData.length).toEqual(3);
+ var resultText = [
+ $(resultData[0]).text(),
+ $(resultData[1]).text(),
+ $(resultData[2]).text()
+ ];
+
+ expect(resultText.sort()).toEqual(["a", "b", "c"]);
+ });
+ });
+ });
+
it("should discard initial elements for ClickMore selector type", function(){
$el.append($("<a>1</a><div>a</div>"));
var moreElements = ['b','c'];
$el.find("a").click(function() {
setTimeout(function() {
+ $el.find("div:contains('a')").remove();
var next = moreElements.shift();
if(next) {
$el.append("<div>"+next+"</div>");
@@ -0,0 +1,29 @@
+describe("UniqueElementList", function () {
+ var $el;
+
+ beforeEach(function () {
+
+ $el = jQuery("#tests").html("");
+ if($el.length === 0) {
+ $el = $("<div id='tests' style='display:none'></div>").appendTo("body");
+ }
+ });
+
+ it("it should add only unique elements", function () {
+
+ $el.html("<a>1</a><a>2</a>");
+
+ var list = new UniqueElementList();
+ expect(list.length).toEqual(0);
+
+ var $a = $el.find("a");
+ list.push($a[0]);
+ expect(list.length).toEqual(1);
+ list.push($a[0]);
+ expect(list.length).toEqual(1);
+ list.push($a[1]);
+ expect(list.length).toEqual(2);
+ list.push($a[1]);
+ expect(list.length).toEqual(2);
+ });
+});

0 comments on commit 2b7c048

Please sign in to comment.