@m10s/cmp
Version:
Package containing scripts used by Schibsteds' sites to integrate with Sourcepoint CMP
45 lines (39 loc) • 3.1 kB
JavaScript
import { setMultipleAttributes } from '../utils';
export function loadMessagingScript(config, _document) {
const wrapperMessage = `${config.baseEndpoint}/unified/wrapperMessagingWithoutDetection.js`;
const allScripts = _document.getElementsByTagName('script');
const curScript = allScripts[allScripts.length - 1];
// preload wrapperMessagingWithoutDetection script
const preloadLink = _document.createElement('link');
setMultipleAttributes(preloadLink, {
rel: 'preload',
as: 'script',
crossorigin: 'crossorigin',
href: wrapperMessage
});
_document.head.appendChild(preloadLink);
// append wrapperMessagingWithoutDetection script
const commonScript = _document.createElement('script');
setMultipleAttributes(commonScript, {
async: true,
src: wrapperMessage
});
curScript.insertAdjacentElement('afterend', commonScript);
}
export function loadTcfApi(_document) {
const tcfScript = _document.createElement('script');
// more details in Sourcepoint docs: https://docs.sourcepoint.com/hc/en-us/articles/4405412395667-CMP-web-implementation#:~:text=The%20first%20part,failure%20of%20service
tcfScript.innerHTML = '"use strict";function _typeof(t){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}!function(){var t=function(){var t,e,o=[],n=window,r=n;for(;r;){try{if(r.frames.__tcfapiLocator){t=r;break}}catch(t){}if(r===n.top)break;r=r.parent}t||(!function t(){var e=n.document,o=!!n.frames.__tcfapiLocator;if(!o)if(e.body){var r=e.createElement("iframe");r.style.cssText="display:none",r.name="__tcfapiLocator",e.body.appendChild(r)}else setTimeout(t,5);return!o}(),n.__tcfapi=function(){for(var t=arguments.length,n=new Array(t),r=0;r<t;r++)n[r]=arguments[r];if(!n.length)return o;"setGdprApplies"===n[0]?n.length>3&&2===parseInt(n[1],10)&&"boolean"==typeof n[3]&&(e=n[3],"function"==typeof n[2]&&n[2]("set",!0)):"ping"===n[0]?"function"==typeof n[2]&&n[2]({gdprApplies:e,cmpLoaded:!1,cmpStatus:"stub"}):o.push(n)},n.addEventListener("message",(function(t){var e="string"==typeof t.data,o={};if(e)try{o=JSON.parse(t.data)}catch(t){}else o=t.data;var n="object"===_typeof(o)&&null!==o?o.__tcfapiCall:null;n&&window.__tcfapi(n.command,n.version,(function(o,r){var a={__tcfapiReturn:{returnValue:o,success:r,callId:n.callId}};t&&t.source&&t.source.postMessage&&t.source.postMessage(e?JSON.stringify(a):a,"*")}),n.parameter)}),!1))};"undefined"!=typeof module?module.exports=t:t()}();';
_document.head.appendChild(tcfScript);
};
export function loadExternalToolsScript(_document, config) {
if (config.disableSentry) return;
const newScript = _document.createElement('script');
setMultipleAttributes(newScript, {
async: true,
type: 'module',
src: 'https://static.pro.cmp.m10s.io/dist/index.js',
fetchPriority: 'low'
});
_document.head.appendChild(newScript);
};