How to select something with its tags? And 1 more question about tag ignoring - iphone

i have to parse google and other search systems search results, and i want to take something with tag, for example i have:
<span class="tl">
<h3 class="r">
<a class="l noline" onmousedown="return rwt(this,'','','','1','AFQjCNE3QDnLsIkujVElxf1vT7LocQ-YYw','','0CCQQFjAA')" target="_blank" href="http://l2.ru/">
<em>Lineage</em>
2. Oficial servers l2, la2,
<em>lineage</em>
2.
</a>
</h3>
<button class="vspib"></button>
</span>
and i want to take all inner content of tag, including , like
<a class="l noline" onmousedown="return rwt(this,'','','','1','AFQjCNE3QDnLsIkujVElxf1vT7LocQ-YYw','','0CCQQFjAA')" target="_blank" href="http://l2.ru/">
<em>Lineage</em>
2. Oficial servers l2, la2,
<em>lineage</em>
2.
</a>
something like
TFHpple *googleParser = [[TFHpple alloc] initWithHTMLData:googleData];
NSArray *linksWithItsNames = [googleParser search:#"//span[#class='tl'/h3[#class='r']"];
TFHppleElement *googleLinkWithItsName = [linksWithItsNames objectAtIndex:0];
NSString *googleFirstLink = [googleLinkWithItsName content];
[myWebView loadHTMLString:googleFirstLink baseURL:requestURL];
And in the end i want to show in my WebView link with its name, that contains in (so i want to ignore all b, em, br etc. tags inside ), and with its #href.
I just saw 1 question - Ignoring / skipping known or all subordinated tags with XPath but don't understand how should i ignore inner tags and how can i take inner text with its tags as a string.
Thank you

TouchXML: an xml parser that will let you access any tag you want.

Related

Fetching dynamic id and url from image with JS

I'm implementing a fancybox into my project and I'm writing a script to automatically wrap an anchor around the images with the url to the image and a "data-fancybox" attribute to let the fancybox script do its thing. However, I'm only getting the url to the very first image, since they all share the same class. There is a dynamic figure id that seems to be the one to get.
My question is - how do I use this figure id to fetch the appropriate img src?
The html is like this:
<figure id="XXXXXXX">
<div>
<img src="image.jpg" />
</div>
</figure>
... other stuff ...
<figure id="YYYYYYY">
<div>
<img src="image2.jpg" alt="">
</div>
</figure>
My code right now is as follows (which works, but only returns the first image url):
$(document).ready(function() {
var src = $("figure img").attr("src");
var a = $("<a/>").attr( { href:src , "data-fancybox":"" } );
$("figure img").wrap(a);
});
I know I can use
var id = $("figure").attr("id");
to get the id I need, but I'm pretty new to coding so I'm not sure how I implement this and use it to get the correct url. Any help is appreciated!
If your goal is to make your images clickable, then you can do smth like this:
$('figure img').each(function() {
$(this).parent().css({cursor: 'pointer'}).attr('data-fancybox', 'gallery').attr('data-src', this.src);
});
DEMO - https://jsfiddle.net/1jznsL7x/
Tip: There is no need to create anchor elements, you can add data-fancybox and data-src attributes to any element and it will work automagically.

Element refreshing in #FindBy with Selenium and java

We have HTML code that has elements which can be expanded or collapsed (depending on what they currently are). They can be expanded only one level. It looks sort of like this
<a href = "#" title = "Expand" id = ...>Row 1 </a>
<a href = "#" title = "Expand" id = ...>Row 2 </a>
<a href = "#" title = "Collapse" id = ...>Row 3 </a>
<a href = "#" id = ...>Subrow 3.1 </a>
<a href = "#" id = ...>Subrow 3.2 </a>
<a href = "#" title = "Expand" id = ...>Row 4 </a>
<a href = "#" title = "Collaps" id = ...>Row 5 </a>
<a href = "#" id = ...>Subrow 5.1 </a>
<a href = "#" id = ...>Subrow 5.2 </a>
There are many more, and it is a bit more complicated than this but you get the picture. Using page object I made a list
#FindBy(xpath = "//a[contains(#title, 'Expand') or contains(#title, 'Collapse')]")
private List<WebElement> expandCollapseElements
And then a method to expand them for instance:
public void expand() {
for (WebElement ele : expndCollapseElements) {
if (ele.getAttribute("title").equals("Expand")) {
ele.click();
}
}
}
In the actual code I also tried waiting until the title changed so I know it worked. Anyway, the problem is that once I expand an element, all the elements underneath apparently become stale. I know when you use #FindBy it refinds the element each time. But I am wondering when the element is refreshed. It seems like it is just done once at the beginning of the loop. Is that true? When is the element list refreshed? And is there a better way to do this? I tried making a new list containing the list and reversing it which kind of worked but not really. The matter is further complicated by the fact that these rows are displayed in its own scrollable div where you need to sacroll to see some of the elements. So any thoughts?
Suggestion: That's the difference between foreach and for.
Try to use "For loop" only.

iMacros - How do I TAG URL with unique surrounding html?

I need to extract "https://www.somesite.com/Some.Name.123" from the code below.
That code segment is repeated many times, and I need the URLs ..Some.Name.X.
There are other code segments between each of the ones I'm interested in, with very different surrounding html. I don't need the ..Some.Name.x URLs in those other segments.
The following is unique to what URLs I need: "<a class="-cx-PRIVATE-uiImageBlock__image"
<div class="clearfix pvm">
<a class="-cx-PRIVATE-uiImageBlock__image -cx-PRIVATE-uiImageBlock__largeImage lfloat"
aria-hidden="true" tabindex="-1" href="https://www.somesite.com/Some.Name.123">
I don't know how to tag that preceding HTML with iMacros, or how to do that with jQuery as the structure will a bit different each time, but you could to this.
Save the web pages with iMacros. Write a program (c, etc.) to read each of the saved files and write the URLs that follow "cx-PRIVATE-uiImageBlock__image" to a file. Add that list of URLs to an iMacro, or have iMacros read the file, and then process each URL from iMacros.
You need to use some scripting.
My answer makes use of jQuery
var listoflinks = []; //array containing your links
$('a[href*="somesite.com"]').each(function () { // for each link that contains somesite.com in href
var j = $(this).attr('href'); //put the whole href in a variable
listoflinks.push(j); // put all values in an array
});
you'll end up with an array that contains all the href values you're looking for.
If you want to see an example and/or you want to play around with the script you can go here:
http://jsfiddle.net/flish/rESjg/
Edited
Your code is still not clear enough, but hopefully this may help
<a class="sibling a" href="link">sibling a</a><br />
<div class="sibling div"><br />
<a class="child a" href="start-with-link/correct-link">Child a</a><br />
</div><br />
Above is the markup I've used. What this means is that I have considered that you have the following elements:
a // with a sibking div
div // with a child a
a // and all of them have appropriate classes
For this markup you can use the following code (jQuery, of course)
var listoflinks = []; //array containing your links
$('a[class="sibling a"]').siblings('div[class="sibling div"]').children('a[class="child a"]').each(function () {
if ((($(this).attr("href")).substring(0,15))=="start-with-link"){
var i = $(this).attr("href");
listoflinks.push(i);
}
});
View detailed example at http://jsfiddle.net/flish/HMXDk/
Be that as it may, you can add more sibling and children elements in case you have other html entities you forgot to mention
<a class="-cx-PRIVATE-uiImageBlock__image" ------------------ <div class="clearfix pvm"> <a class="-cx-PRIVATE-uiImageBlock__image -cx-PRIVATE-uiImageBlock__largeImage lfloat" aria-hidden="true" tabindex="-1" href="somesite.com/some.name.123">
For example, what means ------------------ in your code above?

Insert locallang values into TypoScript [stdWrap]

I wonder if it is possible to insert a locallang value into my typoscript wrap / stdWrap object:
I have:
singleView {
related.wrap = <div class="related">Related items: |</div>
}
What I need is something like that:
singleView {
related.wrap = <div class="related">{LLL:related_items}:|</div>
}
EDIT:
What I am looking for is an TS equivalent for $this->pi_getLL (which works fine).
In our case, we would be replacing values in our extension with
$this->cObj->stdWrap($item, $this->conf['singleView.']['related']);
locallang.xml in extensions /pi1 directory:
<languageKey index="default" type="array">
<label index="related_items">Related items: </label>
</languageKey>
singleView {
related.dataWrap = <div class="related">{LLL:EXT:myextensionkey/pi1/locallang.xml:related_items}:|</div>
}
Of course related_items must be a valid locallang key.
Edit: Do not use wrap with insertData here, because insertData is then run on the whole content, including the user input. If somebody uses {DB:be_users|0|password} inside the content you are wrapping, he can just see the password (hash) of the backend user with uid 0.
Building on the answer of pgampe here, sadly I don't have enough reputation to do it in a comment.
For easier use, do something like this:
constants:
extension.key = myextensionkey
LLL = LLL:EXT:{$extension.key}/Resources/Private/Language/locallang.xlf
and in your typoscript:
singleView {
related.dataWrap = <div class="related">{{$LLL}:related_items}:|</div>
}

Dojo events: getting it to work with dynamically added DOM elements

I have a method of a class as follows:
add_file: function(name, id, is_new){
// HTML: <div class="icon mime zip">name.zip <a>x</a></div>
var components = name.split('.');
var extension = components[components.length-1];
this.container.innerHTML += "<div id='"+id+"' class='icon mime "+extension+"'>"+name+" <a id='remove-"+id+"' href='#remove'>x</a></div>";
// Add event to a tag
dojo.connect(dojo.byId('remove-'+id), 'onclick', function(ev){
// here i am
});
},
All is working well, until I run this method more than once. The first time the event is registered correctly, and clicking the 'x' will run the "here i am" function. However, once I add more than one node (and yes, the ID is different), the event is registered to the last node, but removed from any previous ones.
In affect I have this:
<div id="field[photos]-filelist">
<div id="file1" class="icon mime jpg">file1.jpg <a id="remove-file1" href="#remove">x</a></div>
<div id="file2" class="icon mime jpg">file2.jpg <a id="remove-file2" href="#remove">x</a></div>
</div>
...and the remove link only works for the last node (remove-file2 in this case).
The problem is you are using the innerHTML +=
That is going to take the existing html, convert it to plain markup, and then completely create new nodes from the markup. In the process, all of the nodes with events get replaced with nodes that look exactly the same but are not connected to anything.
The correct way to do this is to use dojo.place(newNodeOrHTML, refNode, positionString)
var myNewHTML = "<div id='"+id+"' class='icon mime "+extension+"'>"+name+" <a id='remove-"+id+"' href='#remove'>x</a></div>"
//This won't work as is breaks all the connections between nodes and events
this.container.innerHTML += myNewHTML;
//This will work because it uses proper dom manipulation techniques
dojo.place(myNewHTML, this.container, 'last');