简而言之 - 我知道不可能从 Google 协作平台中的 iFrame 调查父 URL。
iframe/嵌入内容的内容托管在各处,与网站本身分开。正如您所发现的那样,同源规则会阻止检查。
您的第一个网址“https://n-labp...googleusercontent.com https://n-labp...googleusercontent.com...”是脚本本身的托管位置。脚本的任何输出(例如 HTML)都将显示为来自此处。
您可以使用嵌入功能将 HTML 和 javascript 直接嵌入到站点中。如果你调查一下,你会发现它托管在类似“https://1457130292-atari-embeds.googleusercontent.com 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>
然后在您的 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>");
}
然后发布为网络应用程序。如果您使用临时活动密钥而不是 Google 帐户,则脚本将以您的身份运行,并且可供任何人使用,甚至是匿名的。
您可能已经解决了这个问题,但我希望它对其他偶然发现它的人有用!