#javascript #google-apps-script
Вопрос:
У меня возникла проблема с надстройкой, которую я разрабатываю для 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…
Комментарии:
1. Не могли бы вы включить содержимое
appsscript.json
? И как вы устанавливаете значениеlink
в своейgetLink
функции?2. Ссылка — это просто строка вида leprosy.com/client/web /[хэш] . Я добавил файл appscript.json к вопросу.
3. Здесь та же ошибка, раньше это работало. Я думаю, что что-то изменилось в Chrome…
4. Хорошо, я исправил эту проблему
setOnClickOpenLinkAction(action)
, заменив этоsetOnClickAction(action)