Search code examples
javascriptjquerygoogle-chrome-extensionfirefox-addon

JavaScript Function.prototype.toSource() in Chrome not working


I have a problem with the script below. I'm getting toSource is not defined error in Chrome and found out that toSource() is Firefox specific. I tried JSON.stringify() instead, for which I got undefined error.

var main_tbls = d.querySelectorAll(".bz_buglist");
var first_tbl = main_tbls[0];
first_tbl.id = "bugz_table";
var ext_script = d.createElement("script");
//ext_script.setAttribute("src", "http://www.javascriptkit.com/script/script2/tablefilter.js");
ext_script.setAttribute("type", "text/javascript");
ext_script.setAttribute("language", "javascript");
ext_script.appendChild(d.createTextNode("var TblId, SearchFlt, SlcArgs;TblId = new Array(), SlcArgs = new Array();var colValues = new Array();" +
    setFilterGrid.toSource() +
    AddGrid.toSource() ));
d.body.appendChild(ext_script);


function setFilterGrid(id)
{
    var tbl = grabEBI(id);
    var ref_row, fObj;
    if (tbl != null && tbl.nodeName.toLowerCase() == "table") {
        if (arguments.length > 1) {
            for (var i = 0; i < arguments.length; i++) {
                var argtype = typeof arguments[i];

                switch (argtype.toLowerCase()) {
                    case "number":
                        ref_row = arguments[i];
                        break;
                    case "object":
                        fObj = arguments[i];
                        break;
                } //switch

            } //for
        } //if

        ref_row == undefined ? ref_row = 2 : ref_row = (ref_row + 2);
        var ncells = getCellsNb(id, ref_row);
        tbl.tf_ncells = ncells;
        if (tbl.tf_ref_row == undefined) tbl.tf_ref_row = ref_row;
        tbl.tf_Obj = fObj;
        if (!hasGrid(id)) AddGrid(id);
    } //if tbl!=null
}

Solution

  • While your question is not clear, it appears that you are looking for the Function.prototype.toString() method. This will work in basically all browsers.

    document.body.innerHTML =  '<pre>' + setFilterGrid.toString() +'</pre>';
    
    
    function setFilterGrid(id)
    {
        var tbl = grabEBI(id);
        var ref_row, fObj;
        if (tbl != null && tbl.nodeName.toLowerCase() == "table") {
            if (arguments.length > 1) {
                for (var i = 0; i < arguments.length; i++) {
                    var argtype = typeof arguments[i];
    
                    switch (argtype.toLowerCase()) {
                        case "number":
                            ref_row = arguments[i];
                            break;
                        case "object":
                            fObj = arguments[i];
                            break;
                    } //switch
    
                } //for
            } //if
    
            ref_row == undefined ? ref_row = 2 : ref_row = (ref_row + 2);
            var ncells = getCellsNb(id, ref_row);
            tbl.tf_ncells = ncells;
            if (tbl.tf_ref_row == undefined) tbl.tf_ref_row = ref_row;
            tbl.tf_Obj = fObj;
            if (!hasGrid(id)) AddGrid(id);
        } //if tbl!=null
    }

    Function.prototype.toSource(), on the other hand, is only available in Firefox. Mozilla's documentation is quite clear that it is:

    Non-standard
    This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.