我在为 gmail 开发的附加组件上遇到问题,我需要创建一个文本按钮,单击该按钮后,用户可以转到某个 URL。但是当用户单击它时,控制台上会显示以下消息:
Refused to run the JavaScript URL because it violates the following Content Security Policy directive: "script-src 'report-sample' 'nonce-meBQMySza2c9vxRO9k5EAQ' 'unsafe-inline'". Note that 'unsafe-inline' is ignored if either a hash or nonce value is present in the source list.
Refused to run the JavaScript URL because it violates the following Content Security Policy directive: "script-src 'nonce-meBQMySza2c9vxRO9k5EAQ' 'self' https://apis.google.com https://ssl.gstatic.com https://www.google.com https://www.gstatic.com https://www.google-analytics.com". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution. Note that hashes do not apply to event handlers, style attributes and javascript: navigations unless the 'unsafe-hashes' keyword is present.
Uncaught (in promise) TypeError: Cannot read properties of null (reading 'location')
我尝试过这两种解决方案:
解决方案 1 - 使用 setOpenLink
function getLink(link) {
return CardService.newTextButton()
.setText("Go to URL")
.setOpenLink(CardService.newOpenLink()
.setUrl(link));
}
解决方案 2 - 使用 setOnClickOpenLinkAction
function openLinkCallback(state) {
return CardService.newActionResponseBuilder()
.setOpenLink(CardService.newOpenLink()
.setUrl(state.parameters.link))
.build();
}
function getLink(link) {
var action = CardService.newAction().setFunctionName('openLinkCallback').setParameters({ link: link });
return CardService.newTextButton()
.setText("Go to URL")
.setOnClickOpenLinkAction(action)
}
哪一种是向用户显示链接的正确方式?还有一件事我可能忘记在这里实现吗?
我的 appsscript.json 文件是:
{
"timeZone": "America/Denver",
"dependencies": {},
"addOns": {
"common": {
"homepageTrigger": {
"runFunction": "handleHomePageStart",
"enabled": true
},
"layoutProperties": {
"primaryColor": "#00A82D",
"secondaryColor": "#00A82D"
},
"logoUrl": "https://leprosy.s3.amazonaws.com/outlook-dev/gmail/medium_logo.png",
"name": "leprosy",
"openLinkUrlPrefixes": [
"https://www.leprosy.com/",
"https://leprosy.com/privacy/policy",
"https://leprosy.com/legal/terms-of-service"
],
"universalActions": [],
"useLocaleFromApp": true
},
"gmail": {
"contextualTriggers": [
{
"unconditional": {},
"onTriggerFunction": "handleStart"
}
],
"authorizationCheckFunction": "checkAuth",
"composeTrigger": {
"selectActions": [
{
"text": "Insert note",
"runFunction": "handleStartCompose"
}
],
"draftAccess": "METADATA"
}
},
"calendar": {}
},
"oauthScopes": [
"https://www.googleapis.com/auth/script.storage",
"https://www.googleapis.com/auth/gmail.addons.execute",
"https://www.googleapis.com/auth/gmail.addons.current.message.readonly",
"https://www.googleapis.com/auth/gmail.addons.current.action.compose",
"https://www.googleapis.com/auth/gmail.addons.current.message.metadata",
"https://www.googleapis.com/auth/script.locale",
"https://www.googleapis.com/auth/script.external_request"
],
"exceptionLogging": "CLOUD",
"urlFetchWhitelist": [
"https://www.leprosy.com/oauth",
"https://www.leprosy.com/json",
"https://www.leprosy.com/shard",
"https://ssl.google-analytics.com/collect"
]
}
提前致谢!
更新:似乎这个问题无法在 Firefox 上重现,只能在 Chrome 上重现......
在您的问题中,您没有确切指定要尝试打开的链接,但请确保其网址前缀包含在 appsscript.json 中的“openLinkUrlPrefixes”中。
因此,如果您想打开“https://leprosy.com/a”,您应该将“https://leprosy.com”添加到“openLinkUrlPrefixes”中。可能有帮助。