1
0
Fork 0
mirror of https://github.com/betagouv/mon-entreprise synced 2025-02-09 22:45:01 +00:00
mon-entreprise/site/source/iframe-integration-script.js
Maxime Quandalle c028d89a3e Simplifie l'intégration iframe
L'insertion de l'iframe par le script ne dépend plus d'un identifiant
présent dans le DOM. Permet également l'insertion de plusieurs iframes
sur la même page, ce qui était impossible avant.
2022-02-14 11:25:13 +01:00

73 lines
2.1 KiB
JavaScript

import { hexToHSL } from './hexToHSL'
let script = document.currentScript,
moduleName = script.dataset.module || 'simulateur-embauche',
couleur =
script.dataset.couleur &&
encodeURIComponent(
JSON.stringify(hexToHSL(script.dataset.couleur.toUpperCase()))
),
lang = script.dataset.lang || 'fr',
fr = lang === 'fr',
baseUrl =
script.dataset.iframeUrl ||
(fr ? import.meta.env.VITE_FR_BASE_URL : import.meta.env.VITE_EN_BASE_URL) +
'/iframes/' +
moduleName,
integratorUrl = encodeURIComponent(window.location.href.toString()),
src =
baseUrl +
(baseUrl.indexOf('?') !== -1 ? '&' : '?') +
`couleur=${couleur}&iframe&integratorUrl=${integratorUrl}&lang=${lang}`
const iframe = document.createElement('iframe')
const iframeAttributes = {
id: 'simulateurEmbauche',
src,
style: 'border: none; width: 100%; display: block; height: 700px',
allow: 'clipboard-write',
allowfullscreen: true,
webkitallowfullscreen: true,
mozallowfullscreen: true,
}
for (var key in iframeAttributes) {
iframe.setAttribute(key, iframeAttributes[key])
}
const links = document.createElement('div')
const moduleToSitePath = {
'simulateur-embauche': '/simulateurs/salarié',
'simulateur-autoentrepreneur': '/simulateurs/auto-entrepreneur',
'simulateur-independant': '/simulateurs/indépendant',
'simulateur-dirigeantsasu': '/simulateurs/dirigeant-sasu',
}
const simulateurLink =
import.meta.env.VITE_FR_BASE_URL + moduleToSitePath[moduleName] ?? ''
const url = new URL(simulateurLink)
const params = new URLSearchParams(url.search)
params.append('utm_source', 'iframe')
params.append('utm_medium', 'iframe')
params.append('utm_campaign', 'newtext')
url.search = params.toString()
const simulateurURL = url.toString()
links.innerHTML = `
<div style="text-align: center; margin-bottom: 2rem; font-size: 80%">
Retrouvez ce simulateur et bien d'autres sur
<a href="${simulateurURL}">
mon-entreprise.urssaf.fr
</a>
</div>
`
script.before(iframe)
script.before(links)
window.addEventListener('message', function (evt) {
if (evt.data.kind === 'resize-height') {
iframe.style.height = evt.data.value + 'px'
}
})