google +1 javascript configuration object
Asked Answered
F

2

11

The google +1 button embed code can have a javascript object with configuration (e.g. "{lang:'de'}").

In plain javascript, this object would be created and immediately destroyed, because it is not referenced by anything.

I wonder how do the google scripts access this object?

<script type="text/javascript" src="https://apis.google.com/js/plusone.js">
  {lang: 'de'}
</script>

It seems to work - except when you dynamically write the script tag including the configuration object into the DOM.

Fraise answered 29/6, 2011 at 15:22 Comment(0)
P
4

Widget script run at document.scripts and searching one with string "/js/plusone.js" in scr attribute. After this trim string and create anonymous function. Result of it is object with configuration properties.

if (Gb[u] > 0) {
    for (var Hb = "", Ib = 0; Ib < Gb[u]; Ib++) {
        var Jb = Gb[Ib][nb]("src");
        Jb && Jb[q]("/js/plusone.js") != -1 && (Hb = Q.d.rb(Gb[Ib]));
    }
    Hb = Hb[Ea](/^\s+|\s+$/g, "");
    Hb[q]("{") != 0 && (Hb = "{" + Hb + "}");
    try {
        var Kb = (new Function("return (" + Hb + "\n)"))(), Mb;
        for (Mb in Kb) i.__GOOGLEAPIS.gwidget[Mb] = Kb[Mb];
    } catch (Nb) {}
}
Perlite answered 1/7, 2011 at 13:20 Comment(0)
L
7

As the google source is obfuscated I don't know how they do it. How I would do it is use JQuery to find the tag and then use .innerHTML to get this as a string then use JSON.parse to parse the object safely.

<script ...>
 {"lang" : "de"}
</script>
...
var data= JSON.parse(
    $('script[src="https://apis.google.com/js/plusone.js"]')[0].innerHTML)
alert(data.lang)
Lenna answered 29/6, 2011 at 17:43 Comment(1)
thx! You were right, but I accepted the other answer because it produced the evidence - though at last, I hoped that the answer was different. Its kinda strange method, but it is like it is. Thanks.Fraise
P
4

Widget script run at document.scripts and searching one with string "/js/plusone.js" in scr attribute. After this trim string and create anonymous function. Result of it is object with configuration properties.

if (Gb[u] > 0) {
    for (var Hb = "", Ib = 0; Ib < Gb[u]; Ib++) {
        var Jb = Gb[Ib][nb]("src");
        Jb && Jb[q]("/js/plusone.js") != -1 && (Hb = Q.d.rb(Gb[Ib]));
    }
    Hb = Hb[Ea](/^\s+|\s+$/g, "");
    Hb[q]("{") != 0 && (Hb = "{" + Hb + "}");
    try {
        var Kb = (new Function("return (" + Hb + "\n)"))(), Mb;
        for (Mb in Kb) i.__GOOGLEAPIS.gwidget[Mb] = Kb[Mb];
    } catch (Nb) {}
}
Perlite answered 1/7, 2011 at 13:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.