Do NOT follow this link or you will be banned from the site!
Finding duplicate page elements
Submitted by thedosmann on Wed, 02/27/2013 - 11:14
This is a script that will find duplicate page elements and return the index of where those elements are in the page order. The use case in this example is looking for duplicate images and pushing the index of that image into an array. In the below example image naming convention was image01, image02, image03, etc. Using this snippet you can grab all the document images, look for duplicates, and return where in the page structure the duplicates are.
You could also tweak to find other element duplicates. The main use case would be a dynamic built page where elements are added and you need to know where the duplicates are in the page structure. Once you have that information you can perform any operations you need to on those elements including reordering, calculating, deleting and a number of other code operations.
As long as you know the order of the page elements you can use the index in the array to do what you need with the page elements.
Use structure would be GrabEle.find(arg), vec will hold the index
////we fiest create an array to hold the filenames
var GrabEle=[];
///we then fill that array with the filnames, if we were looking for other elements we would need to restructure our syntax
function getname(){
which=0;
///////////////////////////create array of elements(use case is images image01, image02, image03, etc.)
for (y=0; y<3; y++){
for (x=0; x<5; x++){
var getVal=document.images['image'+y+x].src;
var splitPath = getVal.split('/');
///we stripoff the path leaving just filename
var fileName = splitPath[splitPath.length - 1];
GrabEle[which]=fileName;
which++
}
}
/////the filenames are now in GrabEle(the image names)
//lets do things with that information
caLnum()
}
///////end func getname
function caLnum(){
///we create an array or arrays to play with
var vec=[];
///we now look for duplicates in GrabEle and use a page element (line) to locate
///obviously we need to know what the page structure is to this, we could do some prelimnary looking for that
///or we know in advance where all our divs and other elements are and the order
Array.prototype.find = function ( what, line ) {
///line is page element like div=line, you can structure line1, line2 etc and use as arg also in findin and findplace
for(i in this ) {
if (what=== "imagename"){
///or build array of names to find and run on each array element
if (this[i] === what) {
///put results in arrays
vec.push(i);
///you can also push to several arrays preset into groups and use arguments[1].push(i);
}
}
}
}
////these are extra match finders that can be used in conjuction with find
Array.prototype.findin = function ( what ) {
/////////returns a match in array
for(o in this ) {
if (this[o] == what) {
return what;
}
}
}
Array.prototype.findplace = function ( what )
{///////returns match place num in array
for(o in this ) {
if (this[o] == what) {
return o;
}
}
}
}
by Jim Atkins 'thedosmann' .
Share it now!