| | |
| | | /** Script allowing to use the Web SDK from a WebView */ |
| | | |
| | | // Check if notice was initially displayed |
| | | var noticeReady = false; |
| | | |
| | | /** |
| | | * Initialize the SDK with the specified configuration |
| | | */ |
| | | function initSdk(config, gvl, sdkURL, languageCode) { |
| | | function initSdk(config, gvl, sdkURL, languageCode, isUnderage) { |
| | | if (config) { |
| | | // Set the global window configuration to the config object |
| | | window.didomiConfig = prepareConfigFromMobile(config, languageCode); |
| | | window.didomiConfig = prepareConfigFromMobile(config, languageCode, isUnderage); |
| | | |
| | | // If app, vendors, and iab exist, set the vendorList property |
| | | if ( |
| | |
| | | /** |
| | | * Open the notice screen |
| | | */ |
| | | function openNotice(options) { |
| | | function openNotice(userStatus, options) { |
| | | handleWebSDKEvents(); |
| | | window.didomiOnReady = window.didomiOnReady || []; |
| | | window.didomiOnReady.push(function (Didomi) { |
| | | Didomi.setUserStatus(userStatus); |
| | | if (options != null && options.deepLinkView != null) { |
| | | Didomi.preferences.show( |
| | | options.deepLinkView == 0 ? "purposes" : "vendors" |
| | |
| | | } else { |
| | | Didomi.notice.show(); |
| | | } |
| | | noticeReady = true; |
| | | }); |
| | | } |
| | | |
| | | window.didomiEventListeners = window.didomiEventListeners || []; |
| | | // TODO: Handle all iOS events |
| | | window.didomiEventListeners.push( |
| | | { |
| | | event: 'api.error', |
| | | listener: function ({ id, reason }) { |
| | | if (window.webkit && window.webkit.messageHandlers) { |
| | | window.webkit.messageHandlers.onError.postMessage(reason); |
| | | } else { |
| | | androidInterface.onError(id, reason); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: "consent.changed", |
| | | listener: function (event) { |
| | | // If the change is coming from the WebView we don't want to trigger the message back to the WebView. |
| | | if (event.action == "webview") { |
| | | return; |
| | | } |
| | | |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.consentChanged.postMessage(Didomi.getUserStatus()); |
| | | } else { |
| | | androidInterface.onConsentChanged(JSON.stringify(Didomi.getUserStatus())); |
| | | /** |
| | | * Listen to SDK events |
| | | */ |
| | | function handleWebSDKEvents() { |
| | | window.didomiEventListeners = window.didomiEventListeners || []; |
| | | // TODO: Handle all iOS events |
| | | window.didomiEventListeners.push( |
| | | { |
| | | event: 'api.error', |
| | | listener: function ({ id, reason }) { |
| | | if (window.webkit && window.webkit.messageHandlers) { |
| | | window.webkit.messageHandlers.onError.postMessage(reason); |
| | | } else { |
| | | androidInterface.onError(id, reason); |
| | | } |
| | | } |
| | | }, |
| | | }, |
| | | { |
| | | event: 'notice.clickagree', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.noticeClickAgree.postMessage(""); |
| | | } else { |
| | | androidInterface.onNoticeClickAgree(); |
| | | { |
| | | event: "consent.changed", |
| | | listener: function (event) { |
| | | // If the change is coming from the WebView we don't want to trigger the message back to the WebView. |
| | | if (event.action == "webview") { |
| | | return; |
| | | } |
| | | |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.consentChanged.postMessage(Didomi.getUserStatus()); |
| | | } else { |
| | | androidInterface.onConsentChanged(JSON.stringify(Didomi.getUserStatus())); |
| | | } |
| | | }, |
| | | }, |
| | | { |
| | | event: 'notice.clickagree', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.noticeClickAgree.postMessage(""); |
| | | } else { |
| | | androidInterface.onNoticeClickAgree(); |
| | | } |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'notice.clickdisagree', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.noticeClickDisagree.postMessage(""); |
| | | } else { |
| | | androidInterface.onNoticeClickDisagree(); |
| | | }, |
| | | { |
| | | event: 'notice.clickdisagree', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.noticeClickDisagree.postMessage(""); |
| | | } else { |
| | | androidInterface.onNoticeClickDisagree(); |
| | | } |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'notice.clickmoreinfo', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.noticeClickMoreInfo.postMessage(""); |
| | | } else { |
| | | androidInterface.onNoticeClickMoreInfo(); |
| | | }, |
| | | { |
| | | event: 'notice.clickmoreinfo', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.noticeClickMoreInfo.postMessage(""); |
| | | } else { |
| | | androidInterface.onNoticeClickMoreInfo(); |
| | | } |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'notice.clickviewvendors', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.noticeClickViewVendors.postMessage(""); |
| | | } else { |
| | | androidInterface.onNoticeClickViewVendors(); |
| | | }, |
| | | { |
| | | event: 'notice.clickviewvendors', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.noticeClickViewVendors.postMessage(""); |
| | | } else { |
| | | androidInterface.onNoticeClickViewVendors(); |
| | | } |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'notice.hidden', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | }, |
| | | { |
| | | event: 'notice.hidden', |
| | | listener: function () { |
| | | if (noticeReady && !Didomi.preferences.isVisible()) { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.noticeHidden.postMessage(""); |
| | | } else { |
| | | androidInterface.onNoticeHidden(); |
| | | } |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'notice.shown', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.noticeShown.postMessage(""); |
| | | } else { |
| | | androidInterface.onNoticeShown(); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'preferences.clickagreetoall', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickAgreeToAll.postMessage(""); |
| | | } else { |
| | | androidInterface.onPreferencesClickAgreeToAll(); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'preferences.clickcategoryagree', |
| | | listener: function ({ categoryId }) { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickCategoryAgree.postMessage(categoryId); |
| | | } else { |
| | | androidInterface.onPreferencesClickCategoryAgree(categoryId); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'preferences.clickcategorydisagree', |
| | | listener: function ({ categoryId }) { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickCategoryDisagree.postMessage(categoryId); |
| | | } else { |
| | | androidInterface.onPreferencesClickCategoryDisagree(categoryId); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: "preferences.clickclose", |
| | | listener: function () { |
| | | // Not handled by mobile SDKs |
| | | }, |
| | | }, |
| | | { |
| | | event: 'preferences.clickdisagreetoall', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickDisagreeToAll.postMessage(""); |
| | | } else { |
| | | androidInterface.onPreferencesClickDisagreeToAll(); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'preferences.clickpurposeagree', |
| | | listener: function ({ purposeId }) { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickPurposeAgree.postMessage(purposeId); |
| | | } else { |
| | | androidInterface.onPreferencesClickPurposeAgree(purposeId); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'preferences.clickpurposedisagree', |
| | | listener: function ({ purposeId }) { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickPurposeDisagree.postMessage(purposeId); |
| | | } else { |
| | | androidInterface.onPreferencesClickPurposeDisagree(purposeId); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'preferences.clicksavechoices', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickSaveChoices.postMessage(""); |
| | | } else { |
| | | androidInterface.onPreferencesClickSaveChoices(); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'preferences.clickvendoragree', |
| | | listener: function ({ vendorId }) { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickVendorAgree.postMessage(vendorId); |
| | | } else { |
| | | androidInterface.onPreferencesClickVendorAgree(vendorId); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'preferences.clickvendordisagree', |
| | | listener: function ({ vendorId }) { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickVendorDisagree.postMessage(vendorId); |
| | | } else { |
| | | androidInterface.onPreferencesClickVendorDisagree(vendorId); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'preferences.clickvendorsavechoices', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickVendorSaveChoices.postMessage(""); |
| | | } else { |
| | | androidInterface.onPreferencesClickVendorSaveChoices(); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'preferences.clickviewvendors', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickViewVendors.postMessage(""); |
| | | } else { |
| | | androidInterface.onPreferencesClickViewVendors(); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: "preferences.hidden", |
| | | listener: function () { |
| | | if (Didomi.notice.isVisible()) { |
| | | window.webkit.messageHandlers.noticeHidden.postMessage(); |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesHidden.postMessage(""); |
| | | } else { |
| | | androidInterface.onPreferencesHidden(); |
| | | } |
| | | } else { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.dismissWebView.postMessage(""); |
| | | } else { |
| | | androidInterface.onDismissPreferences(); |
| | | } |
| | | } |
| | | } else { |
| | | androidInterface.onNoticeHidden(); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'notice.shown', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.noticeShown.postMessage(""); |
| | | } else { |
| | | androidInterface.onNoticeShown(); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'preferences.clickagreetoall', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickAgreeToAll.postMessage(""); |
| | | } else { |
| | | androidInterface.onPreferencesClickAgreeToAll(); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'preferences.clickcategoryagree', |
| | | listener: function ({ categoryId }) { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickCategoryAgree.postMessage(categoryId); |
| | | } else { |
| | | androidInterface.onPreferencesClickCategoryAgree(categoryId); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'preferences.clickcategorydisagree', |
| | | listener: function ({ categoryId }) { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickCategoryDisagree.postMessage(categoryId); |
| | | } else { |
| | | androidInterface.onPreferencesClickCategoryDisagree(categoryId); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: "preferences.clickclose", |
| | | listener: function () { |
| | | // Not handled by mobile SDKs |
| | | }, |
| | | }, |
| | | { |
| | | event: 'preferences.clickdisagreetoall', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickDisagreeToAll.postMessage(""); |
| | | } else { |
| | | androidInterface.onPreferencesClickDisagreeToAll(); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'preferences.clickpurposeagree', |
| | | listener: function ({ purposeId }) { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickPurposeAgree.postMessage(purposeId); |
| | | } else { |
| | | androidInterface.onPreferencesClickPurposeAgree(purposeId); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'preferences.clickpurposedisagree', |
| | | listener: function ({ purposeId }) { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickPurposeDisagree.postMessage(purposeId); |
| | | } else { |
| | | androidInterface.onPreferencesClickPurposeDisagree(purposeId); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'preferences.clicksavechoices', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickSaveChoices.postMessage(""); |
| | | } else { |
| | | androidInterface.onPreferencesClickSaveChoices(); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'preferences.clickvendoragree', |
| | | listener: function ({ vendorId }) { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickVendorAgree.postMessage(vendorId); |
| | | } else { |
| | | androidInterface.onPreferencesClickVendorAgree(vendorId); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'preferences.clickvendordisagree', |
| | | listener: function ({ vendorId }) { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickVendorDisagree.postMessage(vendorId); |
| | | } else { |
| | | androidInterface.onPreferencesClickVendorDisagree(vendorId); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'preferences.clickvendorsavechoices', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickVendorSaveChoices.postMessage(""); |
| | | } else { |
| | | androidInterface.onPreferencesClickVendorSaveChoices(); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: 'preferences.clickviewvendors', |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesClickViewVendors.postMessage(""); |
| | | } else { |
| | | androidInterface.onPreferencesClickViewVendors(); |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: "preferences.hidden", |
| | | listener: function () { |
| | | if (Didomi.notice.isVisible()) { |
| | | { |
| | | event: "preferences.shown", |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesHidden.postMessage(""); |
| | | window.webkit.messageHandlers.preferencesShown.postMessage(""); |
| | | } else { |
| | | androidInterface.onPreferencesHidden(); |
| | | } |
| | | } else { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.dismissWebView.postMessage(""); |
| | | } else { |
| | | androidInterface.onDismissPreferences(); |
| | | androidInterface.onPreferencesShown(); |
| | | } |
| | | } |
| | | } |
| | | }, |
| | | { |
| | | event: "preferences.shown", |
| | | listener: function () { |
| | | if (isIOS()) { |
| | | window.webkit.messageHandlers.preferencesShown.postMessage(""); |
| | | } else { |
| | | androidInterface.onPreferencesShown(); |
| | | } |
| | | } |
| | | }, |
| | | ); |
| | | }, |
| | | ); |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | /** |
| | | * Remove all accents and special characters from id field |
| | | * @param {string} id |
| | | * @param {string} id |
| | | * @returns the sanitized id |
| | | */ |
| | | function sanitizeID(id) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * Fix unescaped quotes and html tags. |
| | | * Fix unescaped quotes. |
| | | * @param {string} text |
| | | * @returns the sanitized text |
| | | */ |
| | | function sanitizeText(text) { |
| | | return text |
| | | // Escape unescaped quotes |
| | | .replace(/(?<!\\)(['"])/g, '\$1') |
| | | // Remove html tags |
| | | .replace(/[<>]/g, ''); |
| | | .replace(/(?<!\\)(['"])/g, '\$1'); |
| | | } |
| | | |
| | | /** |
| | |
| | | * Prepare config file to be consumed by the Web SDK, by disabling the features already handled by Mobile SDKs. |
| | | * @param {*} configFromMobile Config provided by mobile. |
| | | * @param {string} languageCode language code to be set in the Config. |
| | | * @param {boolean} isUnderage whether the notice should be underage or not. |
| | | * @returns Config with unneeded features disabled |
| | | */ |
| | | function prepareConfigFromMobile(configFromMobile, languageCode) { |
| | | function prepareConfigFromMobile(configFromMobile, languageCode, isUnderage) { |
| | | if (configFromMobile == null) { |
| | | return null; |
| | | } |
| | |
| | | configFromMobile.app.consentString.signatureEnabled = false; |
| | | } |
| | | |
| | | // Ignore daysBeforeShowingAgain as this is already handled by the mobile SDKs. |
| | | if (configFromMobile.notice) { |
| | | delete configFromMobile.notice.daysBeforeShowingAgain; |
| | | } |
| | | |
| | | if (languageCode) { |
| | | if (languageCode == "pt-PT") { |
| | | // Temporary workaround for https://didomi.atlassian.net/browse/CMP-5552 |
| | | languageCode = "pt"; |
| | | } |
| | | configFromMobile.languages = { enabled: [languageCode], default: languageCode }; |
| | | } |
| | | |
| | | if (isUnderage) { |
| | | configFromMobile.user = configFromMobile.user || {}; |
| | | configFromMobile.user.isUnderage = isUnderage; |
| | | } |
| | | |
| | | return configFromMobile; |
| | | } |
| | | |