Skip to content

Instantly share code, notes, and snippets.

@netsi1964
Created May 14, 2012 06:05
Show Gist options
  • Save netsi1964/2692080 to your computer and use it in GitHub Desktop.
Save netsi1964/2692080 to your computer and use it in GitHub Desktop.
javascript snippets
function toXPath(s) {
s = s.replace(/[<>]/ig, '');
var iAtt = s.indexOf(' ');
var aAtt = s.substring(iAtt+1).split(' ');
var sTag = s.substr(0, iAtt);
for(var i=0; i<aAtt.length; i++) {
aAtt[i] = '@'+aAtt[i].replace(/"/ig, '\'')+((i!=aAtt.length-1) ? ' ' : '');
};
var a = '';
for(var i=0; i<aAtt.length;i++) {
a+=aAtt[i]+((i!=aAtt.length-1) ? ' and ' : '');
};
return sTag+'['+a+']';
};
void(prompt('Enter tag with attributes', toXPath(prompt('XPath', ''))));
void(prompt('The length is:',prompt('Paste string', '').length))
// Find used classes below a specified selector ************************************
var aClassNames = [];
var aElements = [];
var $result = jQuery('*[class]', jQuery(prompt('Enter CSS selector to search below','html'))).each(function() {
var $this = jQuery(this);
var c = $this.attr('class').split(' ');
for(cc =0; cc<c.length; cc++) {
if (typeof aElements[c[cc]]==='undefined') {
aClassNames.push('.'+c[cc]);
aElements[c[cc]] = '.'+c.join('.');
};
};
});
var r = '';
for(a in aElements) {
r+='.'+a+',';
}
prompt('Found classes',r);
// MINIFIED - ready to add as bookmark
javascript:var aClassNames=[];var aElements=[];var $result=jQuery("*[class]",jQuery(prompt("Enter CSS selector to search below","html"))).each(function(){var $this=jQuery(this);var c=$this.attr("class").split(" ");for(cc=0;cc<c.length;cc++)if(typeof aElements[c[cc]]==="undefined"){aClassNames.push("."+c[cc]);aElements[c[cc]]="."+c.join(".")}});var r="";for(a in aElements)r+="."+a+",";void(prompt("Found classes",r));
// ************************************************************************
// Find the highest Z-index on page *********************************************
var iMZ = -1;
var oE = document.querySelectorAll('*');
for(var i=0;i<oE.length; i++) {
var iZ = parseInt(oE[i].style.zIndex);
iMZ = (iMZ<iZ) ? iZ : iMZ;
};
alert('Max Z-index:'+iMZ);
// ********************************************************************************
// MINIFIED - ready to add as bookmark
javascript:var iMZ=-1;var oE=document.querySelectorAll("*");for(var i=0;i<oE.length;i++){var iZ=parseInt(oE[i].style.zIndex);iMZ=iMZ<iZ?iZ:iMZ};void(alert("Max Z-index:"+iMZ))
// Add jQuery to current page
javascript:if (!!!window.jQuery) {var d=document;var s=d.createElement('script');s.src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js';d.body.appendChild(s);void(alert('added jQuery 1.7.2'))}else{;void(alert('allready using jQuery '+jQuery.fn.jquery))}
// Bookmarklet: Force reload
d = document.location;
ds = d + "";
r = parseInt(Math.random() * 1E4);
p = (ds.indexOf("?") === -1 ? "?" : "&") + "fr=";
document.location = ds.indexOf(p) === -1 ? d + p + r : ds.replace(/?fr(=[^&]*)?|^foo(=[^&]*)?&?/g, "&fr=") + r;
void a;
// MINIFIED - ready to add as bookmark
javascript:d=document.location;ds=d+"";r=parseInt(Math.random()*1E4);p=(ds.indexOf("?")===-1?"?":"&")+"fr=";document.location=ds.indexOf(p)===-1?d+p+r:ds.replace(/?fr(=[^&]*)?|^foo(=[^&]*)?&?/g,"&fr=")+r;void a;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment