Google Apps 脚本在 Javascript 中获取 iFrame 的父 URL

Nat*_*ver 7 javascript iframe cross-domain-policy google-apps-script

我已经搜索了很多论坛并且非常有信心这将是一个不,但我想我会向社区开放以防万一;)

我的任务是在我们的 Google 协作平台页面上创建一个工具,用于记录我们员工在访问页面后的访问次数。它有助于确认文档访问和活动日志的合规性。如果 iFrame 与它所在的页面位于同一域中,则从框架内查询父页面的 URL 相当容易,但安全限制会限制跨域或子域的查询。

我希望我将 Google 应用程序脚本嵌入到 Google 网站页面这一事实会给我更多选择。到目前为止,我已经尝试了命令document.referrerparent.document.locationparent.window.document.locationparent.window.locationparent.document.location.href,以及从窗口和文档的角度相同的命令。他们的回答都是一样的:

  https://n-labp6vtqrpsdn12345neycmicqw7krolscvdkda-0lu-script.googleusercontent.com/userCodeAppPanel
Run Code Online (Sandbox Code Playgroud)

当我想要:

 https://sites.google.com/mysite.com/mysite/test/test3
Run Code Online (Sandbox Code Playgroud)

有没有谷歌老手有额外的技巧?

编辑:我刚刚尝试通过 html 链接传递变量,Google 网站上的 Apps 脚本的 Google 图像占位符,并且稍微远了一点。你看,我可以运行这个 url:https : //script.google.com/a/macros/coordinationcentric.com/s/AKfycbxDX2OLs4LV3EWmo7F9KuSFRljMcvYz6dF0Nm0A2Q/exec? test = hello&test2=howareyou 如果我运行 url 并获取变量 test1 和 test2在一个单独的窗口中。如果我尝试将该 URL 嵌入到 Google 协作平台上的 HTML 页面中,则会引发此混合内容错误:

  trog_edit__en.js:1544 Mixed Content: The page at 
 'https://sites.google.com/a/mysite.com/mysite/test/test3' was loaded over HTTPS, but requested an insecure image 'http://www.google.com/chart?chc=sites&cht=d&chdp=sites&chl=%5B%5BGoogle+Apps+Script%27%3D20%27f%5Cv%27a%5C%3D0%2710%27%3D499%270%27dim%27%5Cbox1%27b%5CF6F6F6%27fC%5CF6F6F6%27eC%5C0%27sk%27%5C%5B%22Apps+Script+Gadget%22%27%5D%27a%5CV%5C%3D12%27f%5C%5DV%5Cta%5C%3D10%27%3D0%27%3D500%27%3D197%27dim%27%5C%3D10%27%3D10%27%3D500%27%3D197%27vdim%27%5Cbox1%27b%5Cva%5CF6F6F6%27fC%5CC8C8C8%27eC%5C%27a%5C%5Do%5CLauto%27f%5C&sig=TbGPi2pnqyuhJ_BfSq_CO5U6FOI'. This content should also be served over HTTPS.
Run Code Online (Sandbox Code Playgroud)

有人尝试过这种方法吗?

小智 2

简而言之 - 我知道不可能从 Google 协作平台中的 iFrame 调查父 URL。

iframe/嵌入内容的内容托管在各处,与网站本身分开。正如您所发现的那样,同源规则会阻止检查。

您的第一个 URL“ https://n-labp...googleusercontent.com ...”是脚本本身的托管位置。脚本的任何输出(例如 HTML)都将显示为来自此处。

您可以使用嵌入功能将 HTML 和 javascript 直接嵌入到站点中。如果您进行调查,您会发现它托管在类似“ https://1457130292-atari-embeds.googleusercontent.com ...”的地方

调用parent 将始终给出这个基于*-atari 的URL,而不是它托管的实际页面。

一个相当轻量级的解决方案是结合使用两者。使用简单的 doGet ping 并处理 Apps 脚本中的工作。

在您的网站上,使用嵌入功能插入:

<!DOCTYPE html>
<html>
<body onbeforeunload="return depart()">
<script>
var page = "testpage"; // manually set a name for each page you paste this code in to
var script = "https://script.google.com/macros/s/... your script, ending with exec ...";

fetch(script+"?page="+page+"&direction=arrive");

function depart(){
fetch(script+"?page="+page+"&direction=depart");
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

然后在您的 Apps 脚本中:

function doGet(e){
var httpParams = e.parameter ? e.parameter : "";
// params is an object like {"page": "testpage1", "n": "1"}
var getPage = httpParams.page ? httpParams.page : "";
var getDirection = httpParams.direction ? httpParams.direction : "";

/* Handle it as you please, perhaps like: */

var user = Session.getActiveUser().getEmail();
/* maybe use a temporary active key if open to non-Google users */
/* first-time Google users will have to authenticate, so embed one frame somewhere full-size maybe, or just tell users to go to the script's link */

/* hand off to a helper script */
var time = new Date();
var timeUTC = time.toUTCString(); // I like UTC
doSomethingWithThis(user, direction, timeUTC);
/* etc... */

/* Return some blank HTML so it doesn't look too funny */
return HtmlService.createHtmlOutput("<html><body></body></html>");
}
Run Code Online (Sandbox Code Playgroud)

然后发布为网络应用程序。如果您使用临时活动密钥而不是 Google 帐户,则脚本将以您的身份运行,并且可供任何人使用,甚至是匿名的。

您可能已经解决了这个问题,但我希望它对其他偶然发现它的人有用!