如何通过用户脚本/同步加载谷歌地图API?

GJ.*_*GJ. 13 javascript google-maps userscripts google-maps-api-3

我正在编写一个用户脚本(greasemonkey脚本),需要将谷歌地图添加到我无法控制的网站上的页面.

我试图像这样添加脚本(在加载我尝试的其他脚本时效果很好):

var my_script = document.createElement('script');
my_script.setAttribute('src',
             'https://maps.googleapis.com/maps/api/js?key=API_KEY&sensor=false');
document.head.appendChild(my_script);
Run Code Online (Sandbox Code Playgroud)

但这失败了:

Failed to execute 'write' on 'Document': It isn't possible to write into a 
document from an asynchronously-loaded external script unless it is explicitly
opened.
Run Code Online (Sandbox Code Playgroud)

如何从用户脚本加载和使用maps api?

小智 6

根据文档,
除非您提供参数callback
=> 异步加载Google Maps API,否则每个脚本只能加载API

我没有编写用户脚本的经验,但我希望它有所帮助.

注意:不要忘记将回调函数添加到全局命名空间中.
(在普通JS中,您可以将其添加到窗口对象中.)


abm*_*bmd 0

您可以尝试添加一个'async'属性my_script,如下所示:

my_script.setAttribute('async',true);
Run Code Online (Sandbox Code Playgroud)

您也可以看看这个: https: //developers.google.com/maps/documentation/javascript/tutorial? hl=en#asynch