做这个的起因很简单:我喜欢用Google Chrome,又喜欢逛豆瓣Google Chrome支持User Scripts了,但是我写的那几个插件在Chrome下又跑不了。因为Google ChromeGoogle Gears,据说可以跨域Ajax,于是弄吧!结果越弄越复杂,因为Google Gears的跨域也是有很多限制的,看来还得服务器跑,还好有Google的App Engine,继续弄下去吧!

当然了,现在这个仅仅只是为Google Gears服务,写的很简单,不过还是比较“Ajax”的,看看简单的示例代码:

<script type="text/javascript" src="gears_init.js"></script>
<script type="text/javascript">
    var options = {
        'url': 'http://1986.appspot.com/', //你要请求的地址
        'method': 'GET'
    };
    
    var workerPool = google.gears.factory.create('beta.workerpool');

    workerPool.onmessage = function(a, b, message) {
        //alert('received: ' + message.body);
        document.write(message.body);
    };

    var childWorkerId = workerPool.createWorkerFromUrl('http://ajaxproxy.appspot.com/gears/gears_proxy.js');
    workerPool.sendMessage(options, childWorkerId);
</script>

后续的计划,起码也要实现JSONP吧,这个运用得还是非常广泛的。

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.