2

i'm having next trouble, trying to use JSON.stringify with "replacer".

var scriptTag = document.createElement('script');
        scriptTag.type = 'text/javascript';
        scriptTag.src = 'https://wicked-good-xpath.googlecode.com/files/wgxpath.install.js';
        document.body.appendChild(scriptTag);


function censor(censor) {
  var i = 0;

  return function(key, value) {

console.log(i,typeof(censor),'=====',typeof(value), value);    
    if(i !== 0 && /*typeof(censor) === 'object' && typeof(value) == 'object' && */ censor == value) 
      return null; 
console.log(i,typeof(censor),'=====',typeof(value), value); 
    if(i >= 29) // seems to be a harded maximum of 30 serialized objects?
      return null;
console.log(i,typeof(censor),'=====',typeof(value), value); 
    ++i; // so we know we aren't using the original object anymore

    return value;  
  };
}

XPathResult = document.evaluate('**<SOME XPATH HERE>**', document, null, XPathResult.ANY_TYPE, null);
var actualNode = XPathResult.iterateNext();
                    var result = [];
                    while (actualNode) {
                        result.push(jQuery.makeArray(actualNode));
                        actualNode = XPathResult.iterateNext();
                    }
console.log(result);                
console.log("Result: ", JSON.stringify(result, censor(result)));

or pastebin

The problem is that in DevTools and Firebug the 'JSON.stringify(result, censor(result)));' is different -

Chrome output:

console output of **30** objects

Result:  [[{"width":"","vAlign":"","scope":"col","rowSpan":1,"noWrap":false,"height":"","headers":"","colSpan":1,"chOff":"","ch":"","bgColor":"","axis":"","align":"","abbr":"","cellIndex":4,"spellcheck":true,"isContentEditable":false,"contentEditable":"inherit","children":{"length":0},"outerText":"PUBLICATION DATE","outerHTML":"<th scope=\"col\" class=\"sortDesc byPublicationDate\" style=\"cursor: pointer;\">PUBLICATION DATE</th>","innerText":"PUBLICATION DATE","innerHTML":"PUBLICATION DATE","accessKey":"","hidden":false,"webkitdropzone":null,"draggable":null,"tabIndex":null,"dir":null,"translate":null,"lang":null,"title":null,"id":null,"webkitShadowRoot":null,"webkitPseudo":null,"childElementCount":null,"nextElementSibling":null,"previousElementSibling":null,"lastElementChild":null,"firstElementChild":null,"dataset":null,"classList":null,"className":null,"scrollHeight":null,"scrollWidth":null,"scrollTop":null,"scrollLeft":null,"clientHeight":null,"clientWidth":null,"clientTop":null,"clientLeft":null,"offsetParent":null,"offsetHeight":null,"offsetWidth":null,"offsetTop":null,"offsetLeft":null,"style":null,"tagName":null,"parentElement":null,"textContent":null,"baseURI":null,"localName":null,"prefix":null,"namespaceURI":null,"ownerDocument":null,"attributes":null,"nextSibling":null,"previousSibling":null,"lastChild":null,"firstChild":null,"childNodes":null,"parentNode":null,"nodeType":null,"nodeValue":null,"nodeName":null,"jQuery17109208346924278885":null}]]

and FF output:

*console output of **3** objects

Result: 
[[{"jQuery171005647180282625541":13}]]*

could someone explain me, why it works different in these browsers and how could I modify it so it will work in FF same as is GH? BTW - 'result' itself is the same in both browsers.

P.S. I used that question for censor() creating

Community
  • 1
  • 1
Oleksandr Martynov
  • 320
  • 2
  • 5
  • 19
  • [MDN](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/stringify) says: "*If […] an XML value is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array)*" FF seems to treat it as an object with only the non-DOM-properties – Bergi Mar 22 '13 at 12:53
  • What are you trying to do? DOM results should be serialized to (X)HTML, not to JSON – Bergi Mar 22 '13 at 12:54
  • I'm creating test method in selenium client (on PHP). I'm using executeScript() for running this script and want to get JSON response to parse it into PHP array. – Oleksandr Martynov Mar 22 '13 at 13:38

1 Answers1

0

Use a callback which passes censor by reference rather than by value, then simplify the implementation:

var censor = "";
function censorRun(key, value)
   {
    if(censor == value) 
      return null; 
    else
      return value;  
   }

JSON.stringify(result, censor);
Paul Sweatte
  • 24,148
  • 7
  • 127
  • 265