<?xml version="1.0" encoding="UTF-8"?>
<Module>
  <ModulePrefs title="Google.com vs. Google.cn (pre-monkey dom)"
               directory_title="Google.com vs. Google.cn"
               description="Compare results between Google China and Google in the free world."
               author="Daniel C. Silverstein"
               author_email="cubes.feedback+googlevgoogle@gmail.com"
               author_affiliation="Free Agent"
               author_location="El Cerrito, CA"
               render_inline="optional"
               height="400"/>

<Content type="html">
<![CDATA[
<!--
Copyright © 2006 Daniel C. Silverstein (dans@csua.berkeley.edu)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor,
Boston, MA  02110-1301  USA

This program is also subject to the Google Homepage API Terms and
Condtions located at http://www.google.com/apis/homepage/terms.html.
--><!--

<rdf:RDF xmlns="http://web.resource.org/cc/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<Work rdf:about="">
   <license rdf:resource="http://creativecommons.org/licenses/LGPL/2.1/" />
   <dc:type rdf:resource="http://purl.org/dc/dcmitype/Software" />
</Work>

<License rdf:about="http://creativecommons.org/licenses/LGPL/2.1/">
<permits rdf:resource="http://web.resource.org/cc/Reproduction" />
   <permits rdf:resource="http://web.resource.org/cc/Distribution" />
   <requires rdf:resource="http://web.resource.org/cc/Notice" />
   <permits rdf:resource="http://web.resource.org/cc/DerivativeWorks" />
   <requires rdf:resource="http://web.resource.org/cc/ShareAlike" />
   <requires rdf:resource="http://web.resource.org/cc/SourceCode" />
</License>

</rdf:RDF>

-->
<style>
<!--
div.result__MODULE_ID__ {height: 46%; overflow: auto}
p.defresult__MODULE_ID__ {text-align: center; padding-top: 25%; padding-bottom: 15%}
p.loading__MODULE_ID__ {text-align: center; padding-top: 25%; padding-bottom: 15%}
-->
</style>
<script type="text/javascript"><!--
function submit__MODULE_ID__ (reqs) {
  for (var i = 0; i < reqs.length; i++) {
      reqs[i].canvas.innerHTML = "<p class='loading__MODULE_ID__'>Querying " + reqs[i].src_name + "</p>";

      exec_req__MODULE_ID__(reqs[i]);
  }
}

function exec_req__MODULE_ID__ (req) {
    _IG_FetchContent(req.url, function(response) {
        if (response == "") {
            req.canvas.innerHTML = "error: please try again later"; return;
        }

        filter_hide_canvas__MODULE_ID__ (req.canvas);
        req.canvas.innerHTML = response;
        filter_google_results_dom__MODULE_ID__ (req.canvas);
        // req.canvas.innerHTML = filter_view_source__MODULE_ID__(req.canvas.innerHTML);
        filter_show_canvas__MODULE_ID__ (req.canvas);
    });
}

/* filter_google_results_dom
 * Notes:
 * - The DOM Node interface's appendChild method, which has the subtle
 *   behavior of implicitly removing nodes from the document tree
 *   before reparenting them.  That is, appendChild does not copy
 *   nodes, it moves them. 
 * - getElementsByTagName returns NodeList objects, which are
 *   ``live.''  That is, calling appendChild or removeChild on an
 *   element of a NodeList mutates the list.  Consequently, if you
 *   write a loop like this: for (i = 0; i < myNodeList.length; i++)
 *   and invoke removeChild on elements of myNodeList, it will not
 *   behave the way you expect it to.
 */
function filter_google_results_dom__MODULE_ID__ (canvas) {
    var frag = document.createDocumentFragment();
    var divs = canvas.getElementsByTagName("div");
    var tables = canvas.getElementsByTagName("table");
    var stanzas = [], i = 0;

    // Extract Google Search Results Header Bar
    for (i = 0; i < tables.length; i++) {
        if (("#e5ecf9" == tables[i].bgColor) && ("100%" == tables[i].width)) {
            var tds = tables[i].getElementsByTagName("td");
            var container = tds[1].parentNode.parentNode;
            tds[1].style.paddingLeft = "0.5em";
            container.appendChild(document.createElement("tr"));
            container.lastChild.appendChild(tds[1]); // Wherever you go, there you are
            frag.appendChild(tables[i]);
            break;
        }
    }

    // Extract Search Results
    // nb, Google.cn slips in an extra <p><i> </i></p> at the end of the
    // results.  I'm ignoring it for now.
    for (i = 0; i < divs.length; i++) {
        var pars = divs[i].getElementsByTagName("p");
        if (pars.length > stanzas.length) { stanzas = pars; }
    }

    frag.appendChild(document.createElement("span"));
    frag.lastChild.style.fontSize = "0.8em";
    while (stanzas.length > 0) { frag.lastChild.appendChild(stanzas[0]); }

    canvas.innerHTML = "";
    canvas.appendChild(frag);
}

function filter_view_source__MODULE_ID__ (html) {
    html = html.replace(/&/g, "&amp;");
    html = html.replace(/</g, "&lt;");
    html = html.replace(/>/g, "&gt;");
    return "<code>" + html + "</code>";
}

function filter_hide_canvas__MODULE_ID__ (canvas) {
    // nb, verify this works when position is set/defaults to static
    canvas.style.visibility = "hidden";
}

function filter_show_canvas__MODULE_ID__ (canvas) {
    // nb, verify this works when position is set/defaults to static
    canvas.style.visibility = "visible";
}
--></script>

<div id="searchDiv__MODULE_ID__" style="margin-bottom: -10px">
  <form id="f__MODULE_ID__" name="f" action="#"
        onsubmit="submit__MODULE_ID__(
                  [ { url: 'http://www.google.com/search?hl=en&q=' + _esc(this.q__MODULE_ID__.value) + '&btnG=Google+Search',
                      src_name: 'Google.com',
                      canvas: _gel('fwResDiv'+__MODULE_ID__) },
                    { url: 'http://www.google.cn/search?hl=zh-CN&q=' + _esc(this.q__MODULE_ID__.value)+'&btnG=Google+搜索&meta=',
                      src_name: 'Google.cn',
                      canvas: _gel('cnResDiv'+__MODULE_ID__) } ]);
                  return false;">
    <input id="q__MODULE_ID__" style="width: 75%" name="q" type="text" value="" />
    <input id="s__MODULE_ID__" style="width: 20%" name="s" type="submit" value="Fight!" />
  </form>
</div>
<div id="fwResDiv__MODULE_ID__" class="result__MODULE_ID__"
     style="border-bottom-style: solid; border-width: 1px; border-color: #aaaaaa">
  <p class="defresult__MODULE_ID__">Google Search</p>
</div>
<div id="cnResDiv__MODULE_ID__" class="result__MODULE_ID__">
  <p class="defresult__MODULE_ID__">Google 搜索</p>
</div>
]]>
</Content>
</Module>

