Search code examples
javascriptzabbix

JSON stringify ignoring parameters in different tabs same browser


I need to beautify an existing view through a userscript (TamperMonkey). The code works in JSFiddle (see http://jsfiddle.net/2phrogm5/), but not where I need it: inside the Zabbix web interface.

To replicate the issue:

JSON.stringify({"asd": {"asd": 3}}, null, 4)

Expected result:

"{
    "asd": {
        "asd": 3
    }
}"

My output:

"{"asd":{"asd":3}}"

The issue doesn't exist using Developer Tools on https://stackoverflow.com/.

I already tried the solution provided in JSON.stringify() array bizarreness with Prototype.js, with no success.


Solution

  • Looking at the source code of the Zabbix web interface, you can see where the method is overwritten:

    zabbix-software$ egrep -iR "JSON.stringify *="
    frontends/php/jsLoader.php:             'var _json_stringify = JSON.stringify;'.
    frontends/php/jsLoader.php:             'JSON.stringify = function(value) {'.
    

    The original function is still available, just with a different name: _json_stringify(). The updated jsfiddle is http://jsfiddle.net/u7r8q19g/

    update

    in Zabbix 5 they put the thing back where it came from or so help me, so now I'm doing:

                if (typeof _json_stringify === "function") {
                    item.html(_json_stringify(JSON.parse(text), undefined, 4))
                } else {
                    item.html(JSON.stringify(JSON.parse(text), undefined, 4))
                }
    

    thx to How to tell if a JavaScript function is defined