0

I am trying to replace everything with only images for images.google.com with some search term.

I tried the below code with firebug but says undefined.

document.body.innerHTML=document.getElementsByTagName('img').innerHTML

On the page [undefined] appears

I checked only

document.getElementsByTagName('img').innerHTML

It says undefined.

I checked only

document.getElementsByTagName('img')

It shows me lot of img elements

What is wrong.

Santhosh
  • 9,965
  • 20
  • 103
  • 243

3 Answers3

1

If you want to reduce the contents of the <body> to a particular collection of elements, you could try:

// `slice` to get an `Array` vs. `NodeList`, which may be "live"
var imgs = Array.prototype.slice.call(document.querySelectorAll('img.rg_i'), 0);

// clear out current contents
document.body.innerHTML = '';

// append each `<img>` back into the `<body>`
for (var i = 0, l = imgs.length; i < l; i++) {
    imgs[i].style.display = 'inline';

    document.body.appendChild(imgs[i]);
    document.body.appendChild(document.createTextNode(' '));
}
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
0

Try to do this:

var objs = document.getElementsByTagName('img');
if (objs) {
 // there might be more than one
 document.body.innerHTML = objs[0].parentNode.innerHTML;
}
Noam Rathaus
  • 5,405
  • 2
  • 28
  • 37
  • i tried this it says document.getElementByTagName is not a function. then i tried with document.getElementsByTagName('img').parentNode.innerHTML it says document.getElementsByTagName(...).parentNode is undefined – Santhosh Dec 26 '13 at 10:24
0

Img tags do not have innerHTML and getElementsByTagName is plural.

You can do

var imgs = document.images,html=""; // or document.getElementsByTagName("img");
var div = document.createElement("div");
for (var i=0;i<imgs.length;i++) { 
  div.appendChild(imgs[i]);
}
document.body.innerHTML=div.innerHTML;

Here is an even more google image specific version based on Jonathan's code

I tested it in the Chrome console on a google image search

(function() {
  var links=document.querySelectorAll('a.rg_l'),html=[];
  for (var i=0, n=links.length;i<n;i++) {
    html.push(links[i].innerHTML);
  }
  document.body.innerHTML=html.join(" - "); // or whatever you want between images
  html=null;})()
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • yes this works, but cant we copy paste the original img html content – Santhosh Dec 26 '13 at 10:31
  • Yes, see Jonathan's answer – mplungjan Dec 26 '13 at 10:32
  • as i told i am trying this code from firefox firebug on images.google.com, I tried document.body.innerHTML=document.getElementsById('a').innerHTML; but that is also not working. because the parent element for img is a element – Santhosh Dec 26 '13 at 10:37
  • Who voted down and why? Show yourself and help us understand what the problem is in your eyes! – mplungjan Dec 26 '13 at 20:28