如何将 JavaScript 代码嵌入 Google 协作平台页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10357716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I embed JavaScript code into a Google Sites page?
提问by Aparna Khombhadia
I have a Google Sites page. How can I embed JavaScript code in it, without implementing a gadget and adding JavaScript code in that?
我有一个 Google 协作平台页面。如何在其中嵌入 JavaScript 代码,而无需实现小工具并在其中添加 JavaScript 代码?
回答by Serge Populov
You can run some JavaScript code, butduring page editing it will be automatically wrapped into a trivial gadget, and when rendering page it will be executed inside of iframe, and won't be able to interact with DOM of main page- this is prevented by security mechanisms of browser and Google Sites.
你可以运行一些 JavaScript 代码,但是在页面编辑时它会被自动包装成一个琐碎的小工具,并且在渲染页面时它将在 iframe 内部执行,并且无法与主页的 DOM 交互- 这是被阻止的通过浏览器和 Google 协作平台的安全机制。
So, anything your script do, it will do in a sandbox of it's iframe, who's outer size is defined at page design time, if you don't want to implement a gadget.
因此,您的脚本所做的任何事情,都将在其 iframe 的沙箱中执行,如果您不想实现小工具,iframe 的外部大小是在页面设计时定义的。
And even if you implement a gadget with your script inside, it will also run in the same iframe sandbox. But gadget has some advantages, for example access to API, that allows to change size of iframe dynamically, and some others.
即使您在内部使用脚本实现小工具,它也将在同一个 iframe 沙箱中运行。但是 gadget 有一些优势,例如访问 API,允许动态更改 iframe 的大小,以及其他一些优势。
回答by Chetabahana
Refer to this articleyou need to create a Google Gadget file that you can access from a server to share the file and write the following code:
参考这篇文章你需要创建一个谷歌小工具文件,你可以从服务器访问该文件以共享文件并编写以下代码:
<Module>
<ModulePrefs title="Custom Gadget"/>
<Content type="html">
<![CDATA[
<script src="PUT YOUR SCRIPT HERE"></script>
]]>
</Content>
</Module>
Save the file as Name.xmland upload it to a file hosting service that has public access (not password protected). Then insert the link as following:
将文件另存为Name.xml并将其上传到具有公共访问权限(不受密码保护)的文件托管服务。然后插入链接如下:
Insert > .. More Gadget > Add gadget by URL
插入 > .. 更多小工具 > 通过 URL 添加小工具
Once you copy your url to the saved .xml Google Gadget code, you will have javascript embedded through your very own custom Google Gadget.
将 URL 复制到保存的 .xml Google Gadget 代码后,您将通过自己的自定义 Google Gadget 嵌入 JavaScript。
回答by darrinm
Google has updated their mechanism for embedding HTML and Javascript.
谷歌更新了他们嵌入 HTML 和 Javascript 的机制。
See https://gsuiteupdates.googleblog.com/2017/12/embed-html-and-javascript-in-new-google-sites.html.
请参阅https://gsuiteupdates.googleblog.com/2017/12/embed-html-and-javascript-in-new-google-sites.html。
It is still sandboxed in an iframe with the limitations mentioned in @Serge Populov's answer.
它仍然在 iframe 中进行沙盒处理,并具有@Serge Populov 的回答中提到的限制。
回答by dnozay
You can use the HTML Box
gadget, here is where you find it in the menu:
您可以使用该HTML Box
小工具,您可以在菜单中找到它:
It allows you to Insert custom HTML, CSS, and Javascript.
它允许您插入自定义 HTML、CSS 和 Javascript。
Caveats:
注意事项:
- your HTML is loaded in an
<iframe>
, - your code cannotinteract with content outside of the
HTML Box
- the
HTML Box
does not currently support<iframe>
, - your script cannot create any
<script>
,<img>
or<a>
tags,
- 你的 HTML 被加载到一个
<iframe>
, - 您的代码不能与外部内容交互
HTML Box
- 在
HTML Box
目前不支持<iframe>
, - 您的脚本无法创建任何
<script>
、<img>
或<a>
标签,
回答by Decker W Brower
You can read Googles documentation on how to do this here http://support.google.com/sites/bin/answer.py?hl=en&answer=2500646&ctx=cb&src=cb&cbid=-11zqfs2rw8yfe&cbrank=0
您可以在此处阅读有关如何执行此操作的 Google 文档http://support.google.com/sites/bin/answer.py?hl=en&answer=2500646&ctx=cb&src=cb&cbid=-11zqfs2rw8yfe&cbrank=0