Skip to content

Commit

Permalink
try parse before handling messages
Browse files Browse the repository at this point in the history
  • Loading branch information
vdabar committed Oct 17, 2019
1 parent 5c8f684 commit e80d627
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Config
const TRUSTPILOT_GENERAL_CONFIGURATION = 'general';
const TRUSTPILOT_TRUSTBOX_CONFIGURATION = 'trustbox';
const TRUSTPILOT_INTEGRATION_KEY = 'key';
const TRUSTPILOT_PLUGIN_VERSION = '2.6.495';
const TRUSTPILOT_PLUGIN_VERSION = '2.6.500';
const TRUSTPILOT_SCRIPT = 'TrustpilotScriptUrl';
const TRUSTPILOT_INTEGRATION_APP = 'IntegrationAppUrl';
const TRUSTPILOT_WIDGET_SCRIPT = 'WidgetScriptUrl';
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "trustpilot/module-reviews",
"description": "The Trustpilot Review extension makes it simple and easy for merchants to collect reviews from their customers to power their marketing efforts, increase sales conversion, build their online reputation and draw business insights.",
"type": "magento2-module",
"version": "2.6.495",
"version": "2.6.500",
"license": [
"OSL-3.0"
],
Expand Down
2 changes: 1 addition & 1 deletion view/adminhtml/web/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function receiveInternalData(e) {
const parsedData = {};
if (data && typeof data === 'string' && tryParseJson(data, parsedData)) {
if (parsedData.type === 'updatePageUrls' || parsedData.type === 'newTrustBox') {
this.submitSettings(jsonData);
this.submitSettings(parsedData);
}
}
}
Expand Down
96 changes: 55 additions & 41 deletions view/frontend/templates/head/head.phtml
Original file line number Diff line number Diff line change
@@ -1,53 +1,67 @@
<?php /* @var $block \Trustpilot\Reviews\Block\Head */?>
<script type="text/javascript" async>
var w = document.createElement("script");
var w = document.createElement("script");
w.type = "text/javascript";
w.src = "<?php echo $block->getWidgetScriptUrl(); ?>";
w.async = true;
document.head.appendChild(w);
</script>
<script type="text/javascript">
(function(w,d,s,r,n){w.TrustpilotObject=n;w[n]=w[n]||function(){(w[n].q=w[n].q||[]).push(arguments)};
a=d.createElement(s);a.async=1;a.src=r;a.type='text/java'+s;f=d.getElementsByTagName(s)[0];
f.parentNode.insertBefore(a,f)})(window,document,'script', '<?php echo $block->getScriptUrl(); ?>', 'tp');
tp('register','<?php echo $block->getInstallationKey(); ?>');
(function(w,d,s,r,n){w.TrustpilotObject=n;w[n]=w[n]||function(){(w[n].q=w[n].q||[]).push(arguments)};
a=d.createElement(s);a.async=1;a.src=r;a.type='text/java'+s;f=d.getElementsByTagName(s)[0];
f.parentNode.insertBefore(a,f)})(window,document,'script', '<?php echo $block->getScriptUrl(); ?>', 'tp');
tp('register','<?php echo $block->getInstallationKey(); ?>');
</script>
<script type="text/javascript">
function inIframe () {
try {
return window.self !== window.top;
} catch (e) {
return false;
}
}
function inIframe () {
try {
return window.self !== window.top;
} catch (e) {
return false;
}
}

if (inIframe()) {
window.addEventListener('message', function(e) {
var adminOrign = new URL(window.location).hostname;
if (!e.data || e.origin.indexOf(adminOrign) === -1) {
return;
}
if (typeof TrustpilotPreview !== 'undefined') {
if (typeof e.data === 'string' && e.data === 'submit') {
TrustpilotPreview.sendTrustboxes();
} else {
jsonData = JSON.parse(e.data);
if (jsonData.trustbox) {
TrustpilotPreview.setSettings(jsonData.trustbox);
} else if (jsonData.customised) {
TrustpilotPreview.updateActive(jsonData.customised);
}
}
} else {
var p = document.createElement("script");
p.type = "text/javascript";
p.onload = function () {
const iFrame = e.source.parent.document.getElementById('configuration_iframe').contentWindow;
TrustpilotPreview.init(['<?php echo $block->getPreviewCssUrl(); ?>'], JSON.parse(e.data), iFrame, e.source);
};
p.src = '<?php echo $block->getPreviewScriptUrl(); ?>';
document.head.appendChild(p);
}
});
}
function tryParseJson(str) {
if (typeof str === 'string') {
try {
return JSON.parse(str);
} catch (e) {
return false;
}
}
return false;
}

if (inIframe()) {
window.addEventListener('message', function(e) {
var adminOrign = new URL(window.location).hostname;
if (!e.data || e.origin.indexOf(adminOrign) === -1) {
return;
}
if (typeof TrustpilotPreview !== 'undefined') {
if (typeof e.data === 'string' && e.data === 'submit') {
TrustpilotPreview.sendTrustboxes();
} else {
jsonData = tryParseJson(e.data);
if (jsonData.trustbox) {
TrustpilotPreview.setSettings(jsonData.trustbox);
} else if (jsonData.customised) {
TrustpilotPreview.updateActive(jsonData.customised);
}
}
} else {
var settings = tryParseJson(e.data);
if (settings) {
var p = document.createElement("script");
p.type = "text/javascript";
p.onload = function () {
const iFrame = e.source.parent.document.getElementById('configuration_iframe').contentWindow;
TrustpilotPreview.init(['<?php echo $block->getPreviewCssUrl(); ?>'], settings, iFrame, e.source);
};
p.src = '<?php echo $block->getPreviewScriptUrl(); ?>';
document.head.appendChild(p);
}
}
});
}
</script>

0 comments on commit e80d627

Please sign in to comment.