Changing allowed string value in obfuscated code - javascript

I have this obfuscated code. I just want to change one thing. Since this is a summary code for Blogger, it allows 40 words to be shown in the summary. I want it to be 0 words. All my tries were unsuccessful. I don't know how to deal with obfuscated code. Can anyone help me setting the string value to 0?
<script type='text/javascript'>//<![CDATA[
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('C X(s,n){14 s["\\9\\2\\i\\5\\8\\d\\2"](/<.*?>/15,"")["\\b\\i\\5\\c\\3"](/\\s+/)["\\b\\5\\c\\d\\2"](0,n-1)["\\N\\g\\c\\h"]("\\j")};C 16(a){13 p=S["\\6\\2\\3\\E\\5\\2\\7\\2\\h\\3\\y\\w\\M\\v"](a),z="",G=p["\\6\\2\\3\\E\\5\\2\\7\\2\\h\\3\\b\\y\\w\\D\\8\\6\\Q\\8\\7\\2"]("\\c\\7\\6");U(G["\\5\\2\\h\\6\\3\\e"]>=1){z="\\t\\c\\7\\6\\j\\d\\5\\8\\b\\b\\u\\f\\o\\8\\5\\b\\8\\7\\f\\j\\b\\9\\d\\u\\f"+G[0]["\\b\\9\\d"]+"\\f\\j\\4\\r"}Y{z="\\t\\c\\7\\6\\j\\d\\5\\8\\b\\b\\u\\f\\o\\8\\5\\b\\8\\7\\f\\j\\b\\9\\d\\u\\f\\e\\3\\3\\i\\B\\4\\4\\I\\k\\o\\i\\k\\o\\5\\g\\6\\b\\i\\g\\3\\k\\d\\g\\7\\4\\Z\\10\\2\\O\\q\\q\\v\\18\\q\\3\\i\\L\\4\\K\\2\\L\\17\\W\\1d\\E\\1f\\1g\\1h\\M\\4\\l\\l\\l\\l\\l\\l\\l\\l\\y\\m\\K\\4\\H\\w\\1b\\A\\Q\\9\\P\\O\\P\\A\\H\\4\\b\\I\\19\\J\\J\\4\\h\\g\\12\\c\\7\\8\\6\\2\\k\\N\\i\\6\\f\\j\\4\\r"};p["\\c\\h\\h\\2\\9\\V\\D\\A\\R"]="\\t\\8\\j\\e\\9\\2\\F\\u\\f"+x+"\\f\\r"+z+"\\t\\4\\8\\r"+X(p["\\c\\h\\h\\2\\9\\V\\D\\A\\R"],1a)};$(S)["\\9\\2\\8\\v\\w"](C(){$("\\T\\d\\9\\2\\v\\c\\3")["\\e\\3\\7\\5"]("\\t\\8\\j\\e\\9\\2\\F\\u\\f\\e\\3\\3\\i\\B\\4\\4\\m\\m\\m\\k\\o\\5\\g\\6\\6\\2\\9\\3\\e\\2\\7\\2\\q\\k\\d\\g\\7\\4\\f\\r\\y\\5\\g\\6\\6\\2\\9\\3\\e\\2\\7\\2\\q\\t\\4\\8\\r");1c(C(){U(!$("\\T\\d\\9\\2\\v\\c\\3\\B\\W\\c\\b\\c\\o\\5\\2")["\\5\\2\\h\\6\\3\\e"]){1e["\\5\\g\\d\\8\\3\\c\\g\\h"]["\\e\\9\\2\\F"]="\\e\\3\\3\\i\\B\\4\\4\\m\\m\\m\\k\\o\\5\\g\\6\\6\\2\\9\\3\\e\\2\\7\\2\\q\\k\\d\\g\\7\\4"}},11)});',62,80,'||x65|x74|x2F|x6C|x67|x6D|x61|x72||x73|x69|x63|x68|x22|x6F|x6E|x70|x20|x2E|x41|x77||x62||x39|x3E||x3C|x3D|x64|x79||x42|imgtag|x4D|x3A|function|x54|x45|x66|img|x6B|x31|x30|x55|x38|x49|x6A|x32|x53|x4E|x4C|document|x23|if|x48|x76|stripTags|else|x2D|x51|3000|x2B|var|return|ig|readmore|x4A|x58|x36|42|x47|setInterval|x43|window|x34|x57|x35'.split('|'),0,{}))
//]]></script>
Thanks to commenters, I see that a first step is to pass the code through jsbeautifier.org using the option "Unescape printable chars encoded as \xNN or \uNNNN".
function stripTags(s, n) {
return s["replace"](/<.*?>/ig, "")["split"](/\s+/)["slice"](0, n - 1)["join"](" ")
};
function readmore(a) {
var p = document["getElementById"](a),
imgtag = "",
img = p["getElementsByTagName"]("img");
if (img["length"] >= 1) {
imgtag = "<img class=\"balsam\" src=\"" + img[0]["src"] + "\" />"
} else {
imgtag = "<img class=\"balsam\" src=\"http://1.bp.blogspot.com/-Qe299dX9tp8/Ue8JvCE4W5I/AAAAAAAABwU/kyGMNrS2SMk/s1600/no+image.jpg\" />"
};
p["innerHTML"] = "" + imgtag + "" + stripTags(p["innerHTML"], 42)
};
$(document)["ready"](function () {
$("#credit")["html"]("Bloggertheme9");
setInterval(function () {
if (!$("#credit:visible")["length"]) {
window["location"]["href"] = "http://www.bloggertheme9.com/"
}
}, 3000)
});
Now where do I change the allowed number of words?

Related

javascript get natural width/height of all images in table

I am trying to get the actual image sizes from the images listed in column Image and display it in column Image Size.
The problem I have is that I am only able to get the size of the first image, which is added in each cell for column Image Size.
jsFiddle: https://jsfiddle.net/a92ck0em/
var xmlFile = 'https://raw.githubusercontent.com/joenbergen/files/master/XMLParse2.xml';
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", xmlFile, true);
xhttp.send();
xhttp.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
xmlFunction(this.response);
}
};
}
function xmlFunction(xml) {
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xml, "text/xml");
var table = "<tr><th>Category</th><th>Title</th><th>Image</th><th>Image Size</th></tr>";
var x = xmlDoc.getElementsByTagName("ITEM");
for (var elem of x) {
var titles = elem.getElementsByTagName(
"TITLE")[0].childNodes[0].nodeValue;
var cats = elem.getElementsByTagName("CATEGORY")[0].childNodes[0].nodeValue;
var imageURL = elem.getElementsByTagName("IMAGE").length === 0 ? "..." : elem.getElementsByTagName("IMAGE")[0].getAttribute('url');
var imageSize = elem.getElementsByTagName("IMAGE").length === 0 ? "..." : elem.getElementsByTagName("IMAGE")[0].width + 'x' + [0].height;
table += "<tr><td>" + cats + "</td><td>" + titles + "</td><td>" + "<img src=" + imageURL + ' height="150" width="100">' + '</td><td id="cellId"><textTag>' + "" + "</textTag></td></tr>";
}
document.getElementById("myTable").innerHTML = table;
document.querySelector("img").addEventListener('load', function() {
var imgTags = document.querySelectorAll('img'), i;
for (i = 0; i < imgTags.length; ++i) {
var image_width = document.querySelector("img").naturalWidth;
var image_height = document.querySelector("img").naturalHeight;
}
$('#myTable textTag').each(function() {
$(this).append(image_width + 'x' + image_height);
//console.log(image_width + 'x' + image_height);
});
});
}
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
th,
td {
padding: 5px;
}
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<button type="button" onclick="loadDoc()">Load</button>
<br><br>
<table id="myTable"></table>
Expected:
Ok there are many parts to your question, I started doing the simplest thing, logging that parsed xml document, looks like this:
...
<IMAGE url="...."></IMAGE>
<IMAGESIZE></IMAGESIZE>
...
So in your script you are getting an attribute that does not exist. So you can safely remove this line:
var imageSize = elem.getElementsByTagName("IMAGE").length === 0 ? "..." : elem.getElementsByTagName("IMAGE")[0].width + 'x' + [0].height;
Next you need to check if the images are loaded, simplest thing to do:
"<img src=" + imageURL + ' height="150" width="100" onload="this._loaded = true;">' +
this will add a proprietary _loaded property to it.
Next you need to check when the images are loaded and once loaded fire a function, you need a recursive function to do that which does not blow the stack:
var imgTags = document.getElementsByTagName('img');
function isLoaded(){
if(Array.prototype.slice.call(imgTags).some(function(d,i){return !d._loaded})){
setTimeout(isLoaded,4);
} else {
$('#myTable textTag').each(function(i,node) {
node.textContent = imgTags[i].naturalWidth + 'x' + imgTags[i].naturalHeight;
//console.log(image_width + 'x' + image_height);
});
}
};
isLoaded();
I removed the querySelectorAll(img) bit, it is slower compared to getElementsByTagName and does not return a LIVE HTML Collection, returns a NodeList. The loaded function will fire your Jquery thing once it detects all the _loaded properties. The Array.prototype.slice.call is an old school way to convert HTML Collection or NodeList to a regular array, the cool kids you Array.from nowadays, it is up to you. You might also optimize the above function a bit by storing the result of Array.prototype.slice.call... once, I leave that to you. All in all it looks like this:
https://jsfiddle.net/ibowankenobi/h9evc81a/
The problem is that you need to wait for the images to load before querying properties like width or height.
Normally this is done by setting an onload event handler that updates the display of these properties.
Moreover you're making a loop setting two variables and then, outside of the loop, you're assigning all the images the value of the very same variables.
You need to do the querying inside the second loop instead.

How to divide pasted text from textarea into <p> paragraphs that consist of the same number of characters?

JSFiddle Link
The JSFiddle that I am using seems to be exactly what I need for my project. However, how would I change this current code to ensure each divided paragraph consists of the same amount of characters and all the paragraphs are the same width?
Any help would much be appreciated, especially an updated JSFiddle would be extremely helpful.
$(function() {
$('button').on('click', function() {
var theText = $('textarea').val();
var i = 200;
while (theText.length > 200) {
console.log('looping');
while (theText.charAt(i) !== '.') {
i++;
}
console.log(i);
$("#text_land").append("<p>" + theText.substring(0, i + 1) + "</p>");
theText = theText.substring(i + 1);
i = 200;
}
$('#text_land').append("<p>" + theText + "</p>");
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
<div id="text_land" style="border:1px solid #ccc; padding:25px; margin-bottom:30px;">xzxz</div>
<textarea style="widht:95%;"></textarea>
<button>Go</button>
</div>
If all paragraphs must have the same number of charachters but also the same width, then each character must have the same width, and you have to use a mono-spaced font.
With this code you cut the input text into equal chunks and they display in paragraphs with equal widths.
$(function () {
$('button').on('click', function () {
var theText = $('textarea').val();
var numberOfCharacters = 50;
while (theText.length) {
$("#text_land").append("<p>" + theText.substring(0, numberOfCharacters - 1) + "</p>");
theText = theText.substring(numberOfCharacters);
}
})
})
Of course, this doesn't cut the text on the period character like your fiddle, and it may cut in the middle of words, but you can't have your cookie and eat it too.
If you want to ensure that at least words are not cut in the middle, you could loosen your number-of-character-per-line constraint until you find a space like this..
$(function () {
$('button').on('click', function () {
var theText = $('textarea').val();
var numberOfCharacters = 20;
while (theText.length) {
while (theText.length > numberOfCharacters && theText.charAt(numberOfCharacters) !== ' ') {
numberOfCharacters++;
}
$("#text_land").append("<p>" + theText.substring(0, numberOfCharacters) + "</p>");
theText = theText.substring(numberOfCharacters);
}
})
})

Checking a div for duplicates before appending to the list using jQuery

This should be trivial but I'm having issues...
Basically what I am trying to do is append a new "div" to "selected-courses" when a user clicks on a "course". This should happen if and only if the current course is not already in the "selected-courses" box.
The problem I'm running into is that nothing is appended to the "selected-courses" section when this is executed. I have used alert statements to make sure the code is in fact being run. Is there something wrong with my understanding of the way .on and .each work ? can I use them this way.
Here is a fiddle http://jsfiddle.net/jq9dth4j/
$(document).on("click", "div.course", function() {
var title = $( this ).find("span").text();
var match_found = 0;
//if length 0 nothing in list, no need to check for a match
if ($(".selected-course").length > 0) {
match_found = match(title);
}
if (matched == 0) {
var out = '<div class="selected-course">' + '' + title + ''+'</div>';
$("#selected-box").append(out);
}
});
//checks to see if clicked course is already in list before adding.
function match(str) {
$(".selected-course").each(function() {
var retval = 0;
if(str == this.text()) {
//course already in selected-course section
retval = 1;
return false;
}
});
return retval;
}
There was a couple of little issues in your fiddle.
See fixed fiddle: http://jsfiddle.net/jq9dth4j/1/
function match(str) {
var retval = 0;
$(".selected-course").each(function() {
if(str == $(this).text()) {
retval = 1;
return false;
}
});
return retval;
}
You hadn't wrapped your this in a jquery object. So it threw an exception saying this had no method text().
Second your retval was declared inside the each so it wasn't available to return outside the each, wrong scope.
Lastly the if in the block:
if (matched== 0) {
var out = '';
out += '<div class="selected-course">' + '' + title + ''+'</div>';
$("#selected-box").append(out);
}
was looking at the wrong variable it was looking at matched which didn't exist causing an exception.
Relying on checking what text elements contain is not the best approach to solve this kind of question. It is prone to errors (as you have found out), it can be slow, it gives you long code and it is sensitive to small changes in the HTML. I would recommend using custom data-* attributes instead.
So you would get HTML like this:
<div class="course" data-course="Kite Flying 101">
<a href="#">
<span>Kite Flying 101</span>
</a>
</div>
Then the JS would be simple like this:
$(document).on('click', 'div.course', function() {
// Get the name of the course that was clicked from the attribute.
var title = $(this).attr('data-course');
// Create a selector that selects everything with class selected-course and the right data-course attribute.
var selector = '.selected-course[data-course="' + title + '"]';
if($(selector).length == 0) {
// If the selector didn't return anything, append the div.
// Do note that we need to add the data-course attribute here.
var out = '<div class="selected-course" data-course="' + title + '">' + title + '</div>';
$('#selected-box').append(out);
}
});
Beware of case sensitivity in course names, though!
Here is a working fiddle.
Try this code, read comment for where the changes are :
$(document).on("click", "div.course", function () {
var title = $(this).find("span").text().trim(); // use trim to remove first and end whitespace
var match_found = 0;
if ($(".selected-course").length > 0) {
match_found = match(title);
}
if (match_found == 0) { // should change into match_found
var out = '';
out += '<div class="selected-course">' + '' + title + '' + '</div>';
$("#selected-box").append(out);
}
});
function match(str) {
var retval = 0; // this variable should place in here
$(".selected-course").each(function () {
if (str == $(this).find('a').text().trim()) { // find a tag to catch values, and use $(this) instead of this
retval = 1;
return false;
}
});
return retval; // now can return variable, before will return undefined
}
Updated DEMO
Your Issues are :
1.this.text() is not valid. you have to use $(this).text().
2.you defined var retval = 0; inside each statement and trying to return it outside each statement. so move this line out of the each statement.
3.matched is not defined . it should be match_found in line if (matched == 0) {.
4. use trim() to get and set text, because text may contain leading and trailing spaces.
Your updated JS is
$(document).on("click", "div.course", function () {
var title = $(this).find("span").text();
var match_found = 0;
if ($(".selected-course").length > 0) {
match_found = match(title);
}
if (match_found == 0) {
var out = '<div class="selected-course">' + '' + title + '' + '</div>';
$("#selected-box").append(out);
}
});
function match(str) {
var retval = 0;
$(".selected-course").each(function () {
if (str.trim() == $(this).text().trim()) {
retval = 1;
return false;
}
});
return retval;
}
Updated you Fiddle

Is it possible to make a word spell out in the page delayed with javascript?

Strange question I know. I am trying to get a word to load letter by letter on a page with a small delay. Yes this is what I think about sometimes when I am coding at work. I have tried numerous methods with absolutely no success. The best I got was an alert box with the following code but I want to do it with the html on the page. Is this even possible?
<script type="text/javascript">
var foo = "foo bar";
foo = foo.length ;
for(i= 1; i<=foo; i++){
var hello = "FOO BAR";
hello = hello.substring(0, i);
alert(hello);
}
</script>
I am assuming there has to be some type of set time out with a hide show and a div to load it into?
You can try something like this:
var word = "Hello World";
function nextChar(i){
$("#word").text("The Word is: [" + i + "]" + word.substring(0, i));
if(i < word.length){
setTimeout("nextChar(" + (i + 1) + ")", 1000);
}
}
$(document).ready(function(){
nextChar(0);
});
And the HTML:
<div id="word"></div>
Let's say you want to load "foo bar" into this div, one character at a time, with a 1 second delay in between.
<div id="destination" />
$(function () {
loadWord("foo bar", 0);
});
function loadWord(word, index) {
if (index == word.length) return;
$("#destination").text($("#destination").text() + word[index]);
setTimeout(function() { loadWord(word, index + 1); }, 1000);
}
A bit more jQuery-ish answer
Major differences from others (beside being jQuery and general):
1) animation
2) DOM manipulation on load only
3) preset width (will not push other things)
Here's a fancier version that makes a jQuery plug-in for adding text to any object and it fades in each successive letter. You can see it work here: http://jsfiddle.net/jfriend00/yMJsc/.
You call it like this:
$(".myClass").revealText("The quick brown fox jumped over the fence.", 500);
And, here's the code to implement the jQuery plugin.
$.fn.revealText = function(text, duration) {
duration = duration || 250;
for (var i = 0; i < this.length; i++) {
showNextChar(this[i], 0);
}
function showNextChar(item, len) {
var base = text.substr(0, len);
var nextChar = text[len];
// don't fade in a space since it wouldn't be visible
var aheadChar = text[len + 1];
if (aheadChar == " ") {
nextChar += aheadChar;
++len;
}
item.innerHTML = base + '<span class="fadeLetter" style="display: none;">' + nextChar + '</span>';
++len;
$(".fadeLetter", item).fadeIn(duration, function() {
if (len < text.length) {
showNextChar(item, len);
} else {
item.innerHTML = text;
}
});
}
return(this);
}

Javascript - Get Meta Keywords and If Non Don't Break Bookmarklet

I have a bookmarklet that gets the meta keywords on a page. However if the there are no meta keywords the bookmarklet breaks.
Here is my current javascript
function docoument_keywords(){
var keywords;
var metas = document.getElementsByTagName('meta');
for (var x=0,y=metas.length; x<y; x++) {
if (metas[x].name.toLowerCase() == "keywords") {
keywords = metas[x];
}
}
return keywords.content;
}
k = document_keywords();
$('body').append("<p>" + k + "</p><p>Content</p>");
The bookmarklet works fine when there are actually keywords in the meta keywords. However its break when there are none. You guys have any solutions?
Much appreciated!
function document_keywords(){
var keywords = '';
var metas = document.getElementsByTagName('meta');
if (metas) {
for (var x=0,y=metas.length; x<y; x++) {
if (metas[x].name.toLowerCase() == "keywords") {
keywords += metas[x].content;
}
}
}
return keywords != '' ? keywords : false;
}
k = document_keywords();
if (k) {
$('body').append("<p>"+k+"</p>");
}
Working example: JS Fiddle
Though Michael answered your question, I just wanted to point out that since you appear to already be using jQuery, this can be accomplished much, much more simply. Your script can be summarized thusly in its entirety:
$('body').append(
"<p>" +
$.map($('meta[name="keywords"]'), function(metaEl) { return metaEl.content; }) +
"</p>"
);
// Or, on one line:
$('body').append("<p>" + $.map($('meta[name="keywords"]'), function(metaEl) { return metaEl.content; }) + "</p>");
Hope it's helpful!

Categories