谷歌地图从Firefox插件(无SDK) [英] Google Maps from Firefox addon (without SDK)

查看:157
本文介绍了谷歌地图从Firefox插件(无SDK)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的adddon中添加一张地图,并且我知道如何在普通网页中执行所需操作,就像我在这里所做的那样: http://jsfiddle.net/hCymP/6/

问题是我真的不知道如何去在Firefox的插件相同。我tryed与 LoadSubScript 和进口也将脚本尝试在下一行添加Chrome html:

 < script src =https://maps.googleapis.com/地图/ API / JS v = 3.exp&安培;传感器=假>< /脚本> 

但没有任何效果。我找到的最佳解决方案是在添加部分代码这个文件(脚本src的代码)在我的函数,导入这个文件 loadSubScript ,我所有的函数都被执行,但是返回一个空的div。

  Components.utils。进口( 资源://gre/modules/Services.jsm); 

window.google = {};
window.google.maps = {};
window.google.maps.modules = {};
var modules = window.google.maps.modules;
var loadScriptTime =(new window.Date).getTime();
$ b $ window.google.maps .__ gjsload__ = function(name,text){modules [name] = text;};
window.google.maps.Load = function(apiLoad){
delete window.google.maps.Load;
apiLoad([0.009999999776482582,[[[ https://mts0.googleapis.com/vt?lyrs=m@227000000\\\&src=api\\\&hl=en-US\\\&,请https: //mts1.googleapis.com/vt?lyrs=m@227000000\\\&src=api\\\&hl=en-US\\\& ],NULL,NULL,NULL,NULL, M @ 2.27亿 ],[[ https://khms0.googleapis.com/kh?v=134\\\&hl=en-US\\\&\",\"https://khms1.googleapis.com/kh?v=134\\\&hl=en- US\\\& ],NULL,NULL,NULL,1, 134 ],[[ https://mts0.googleapis.com/vt?lyrs=h@227000000\\\&src=api\\\&hl=en -US\\\&\" , https://mts1.googleapis.com/vt?lyrs=h@227000000\\\&src=api\\\&hl=en-US\\\&],NULL,NULL,NULL,NULL , H @ 2.27亿],[[ https://mts0.googleapis.com/vt?lyrs=t@131,r@227000000\\\&src=api\\\&hl=en-US\\\&, https://mts1.googleapis.com/vt?lyrs=t@131,r@227000000\\\&src=api\\\&hl=en-US\\\&\"],null,null,null,null,\"t@ 131,R @ 2.27亿 ],NULL,NULL,[ https://cbks0.googleapis.com/cbk 请 https://cbks1.googleapis的.com / CBK? ]],[[ https://khms0.googleapis.com/kh?v=80\\\&hl=en-US\\\&\" ,https://khms1.googleapis.com/ KH 2 v = 80\\\&hl =烯US\\\& ],NULL,NULL,NULL,NULL, 80 ],[[ https://mts0.googleapis.com/mapslt?hl=en- US\\\&\" , https://mts1.googleapis.com/mapslt?hl=en-US\\\&]],[[https://mts0.googleapis.com/mapslt/ft?hl= EN-US\\\&\" , https://mts1.googleapis.com/mapslt/ft?hl=en-US\\\&],[https://mts0.googleapis.com/vt? HL =烯US\\\&\" , https://mts1.googleapis.com/vt?hl=en-US\\\&]],[[https://mts0.googleapis.com/mapslt/织机HL =烯US\\\&\" , https://mts1.googleapis.com/mapslt/loom?hl=en-US\\\&]],[[HTTPS:?//mts0.googleapis。 ?COM / mapslt HL =烯US\\\&\" , https://mts1.googleapis.com/mapslt?hl=en-US\\\&]],[[HTTPS://mts0.googleapis。 COM / mapslt /英尺?HL = EN-US\\\&\" , https://mts1.googleapis.com/mapslt/ft?hl=en-US\\\&]]],[ EN-US , 美,NULL,0,NULL,NULL, https://maps.gstatic.com/mapfiles/, https://csi.gstatic.com,HT TPS://maps.googleapis.com, https://maps.googleapis.com],[ https://maps.gstatic.com/intl/en_us/mapfiles/api-3/13/11 3.13.11],[3047554353],1.0,NULL,NULL,NULL,NULL,1 ,NULL,NULL,1,https://khms.googleapis.com/mz?v=134\\ \\ u0026,null,https://earthbuilder.googleapis.com,https://earthbuilder.googleapis.com,null,https://mts.googleapis.com/vt/icon],loadScriptTime );
};


//我不能使用document.write,但使用loadSubScript insthead
Services.scriptloader.loadSubScript(chrome:// googleMaps / content / Google-Maps-V3 .js,窗口,utf8); //chrome://MoWA/content/Google-Maps-V3.js,window,utf8);

var mapContainer = window.content.document.createElement('canvas');
mapContainer.setAttribute('id',map);
mapContainer.setAttribute('style',width:500px; height:300px);
mapContainer.style.backgroundColor = red;

var mapOptions = {
center:new window.google.maps.LatLng(latitude,longitude),
zoom:5,
mapTypeId: window.google.maps.MapTypeId.ROADMAP


var map = new window.google.maps.Map(mapContainer,mapOptions);
return mapContainer;

您可以帮我吗?我正在开发一个Firefox for Android插件,这就是为什么我需要做* window.content。* document.createElement,因为文档没有声明,只有窗口,我认为这可能是问题...但是我不能声明一切,如果我不知道什么谷歌地图使用。



补充:我也读过Google M aps API团队具有特定的代码,不允许您在本地复制主脚本。特别是,该代码每隔几个小时就过期。我结合了此脚本的一部分: https:// maps.googleapis.com/maps/api/js?v=3.exp&sensor=false ,因为我无法直接执行此操作(错误:在未实现接口HTMLDocument的对象上调用write)。所以我没有任何其他的选择!

解决方案

使用 iframe (在XUL中键入= content)来显示网页内容。在那里你可以包括你喜欢的任何脚本。在 iframe 中的内容不会有任何特殊的权限,或者至少不应该。如果您需要与代码的特权附加部分进行通信,则可以使用例如常规的HTML事件( createEvent addEventListener 和好友)或 postMessage web API来传递消息。



不要尝试将远程代码直接加载到其他页面,或者更糟糕的情况下加载到浏览器中,因为这是兼容性和安全性的噩梦。
由于加载的远程代码和/或代码没有正确地审查在特权上下文中运行,平台将拒绝从远程源(http等)通过loadSubScript等加载这样的脚本。



需要注意的是,如果您稍后想在addons.mozilla.org上托管您的附加组件,并且仍然在特权代码中包含远程脚本,那么您的附加组件将被拒绝,直到你修复它。
另外,即使您在别处托管,如果发现附件中存在已知的安全漏洞,每个附加指南


I need to add a map in my adddon and I know how to do what I need in a "common webpage", like I did here: http://jsfiddle.net/hCymP/6/

The problem is I really don't know how to to the same in a Firefox Addon. I tryed importing the scripts with LoadSubScript and also tryed adding a chrome html with the next line:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>

But nothing works. The best solution I found was to add part of the code in this file (the code of the script src) in my function, to import this file with loadSubScript, and all my function is executed but an empty div is returned.

Components.utils.import("resource://gre/modules/Services.jsm");

window.google = {};
window.google.maps = {};
window.google.maps.modules = {};
var modules = window.google.maps.modules;
var loadScriptTime = (new window.Date).getTime();

window.google.maps.__gjsload__ = function(name, text) { modules[name] = text;};
window.google.maps.Load = function(apiLoad) {
    delete window.google.maps.Load;
    apiLoad([0.009999999776482582,[[["https://mts0.googleapis.com/vt?lyrs=m@227000000\u0026src=api\u0026hl=en-US\u0026","https://mts1.googleapis.com/vt?lyrs=m@227000000\u0026src=api\u0026hl=en-US\u0026"],null,null,null,null,"m@227000000"],[["https://khms0.googleapis.com/kh?v=134\u0026hl=en-US\u0026","https://khms1.googleapis.com/kh?v=134\u0026hl=en-US\u0026"],null,null,null,1,"134"],[["https://mts0.googleapis.com/vt?lyrs=h@227000000\u0026src=api\u0026hl=en-US\u0026","https://mts1.googleapis.com/vt?lyrs=h@227000000\u0026src=api\u0026hl=en-US\u0026"],null,null,null,null,"h@227000000"],[["https://mts0.googleapis.com/vt?lyrs=t@131,r@227000000\u0026src=api\u0026hl=en-US\u0026","https://mts1.googleapis.com/vt?lyrs=t@131,r@227000000\u0026src=api\u0026hl=en-US\u0026"],null,null,null,null,"t@131,r@227000000"],null,null,[["https://cbks0.googleapis.com/cbk?","https://cbks1.googleapis.com/cbk?"]],[["https://khms0.googleapis.com/kh?v=80\u0026hl=en-US\u0026","https://khms1.googleapis.com/kh?v=80\u0026hl=en-US\u0026"],null,null,null,null,"80"],[["https://mts0.googleapis.com/mapslt?hl=en-US\u0026","https://mts1.googleapis.com/mapslt?hl=en-US\u0026"]],[["https://mts0.googleapis.com/mapslt/ft?hl=en-US\u0026","https://mts1.googleapis.com/mapslt/ft?hl=en-US\u0026"]],[["https://mts0.googleapis.com/vt?hl=en-US\u0026","https://mts1.googleapis.com/vt?hl=en-US\u0026"]],[["https://mts0.googleapis.com/mapslt/loom?hl=en-US\u0026","https://mts1.googleapis.com/mapslt/loom?hl=en-US\u0026"]],[["https://mts0.googleapis.com/mapslt?hl=en-US\u0026","https://mts1.googleapis.com/mapslt?hl=en-US\u0026"]],[["https://mts0.googleapis.com/mapslt/ft?hl=en-US\u0026","https://mts1.googleapis.com/mapslt/ft?hl=en-US\u0026"]]],["en-US","US",null,0,null,null,"https://maps.gstatic.com/mapfiles/","https://csi.gstatic.com","https://maps.googleapis.com","https://maps.googleapis.com"],["https://maps.gstatic.com/intl/en_us/mapfiles/api-3/13/11","3.13.11"],[3047554353],1.0,null,null,null,null,1,"",null,null,1,"https://khms.googleapis.com/mz?v=134\u0026",null,"https://earthbuilder.googleapis.com","https://earthbuilder.googleapis.com",null,"https://mts.googleapis.com/vt/icon"], loadScriptTime);
};


//I can't use document.write but use loadSubScript insthead
Services.scriptloader.loadSubScript("chrome://googleMaps/content/Google-Maps-V3.js", window, "utf8"); //chrome://MoWA/content/Google-Maps-V3.js", window, "utf8");

var mapContainer = window.content.document.createElement('canvas');
    mapContainer.setAttribute('id', "map");
    mapContainer.setAttribute('style',"width: 500px; height: 300px");
    mapContainer.style.backgroundColor = "red";

var mapOptions = {
    center: new window.google.maps.LatLng(latitude, longitude),
    zoom: 5,
    mapTypeId: window.google.maps.MapTypeId.ROADMAP
}

var map = new window.google.maps.Map(mapContainer,mapOptions);
return mapContainer;

Can you help me? I'm developing a "Firefox for Android" addon and that's why I need to do things like *window.content.*document.createElement because document is not declared, only window and I think thats may be the problem... But I can't declare everything if I don't know what Google Maps uses.

Added: I also read that Google Maps API Team has specific code that disallows you from copying the main script locally. In particular, that code "expires" every so many hours. I'm combined part of this script: https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false because I can't execute this directly (Error: write called on an object that does not implement interface HTMLDocument). So I don't have any alternative!

解决方案

Use an iframe (type=content if in XUL) to display web content. There you can include whatever scripts you like. The content in the iframe will not have any special privileges, or at least should not. If you need to communicate with the privileged add-on part of your code, you can use e.g. regular HTML events (createEvent, addEventListener and friends) or the postMessage web API to pass messages.

Do not try to load remote code directly into other pages, or worse, into the browser, as this is a compatibility and security nightmare. Because loading remote code and/or code not properly reviewed for running in a privileged context, the platform will refuse to load such scripts from remote sources (http, etc.) via loadSubScript, etc.

Should be noted, that if you'd later like to host your add-on on addons.mozilla.org and still do include remote scripts in privileged code, your add-on will be rejected until you fix it. Also, mozilla might blocklist your add-on even if you host elsewhere if it is discovered that there are known security vulnerabilities in your add-on, per the Add-on Guidelines.

这篇关于谷歌地图从Firefox插件(无SDK)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆