Skip to content
Code-Schnipsel Gruppen Projekte
Commit c07b04a7 erstellt von Jan Wennrich's avatar Jan Wennrich
Dateien durchsuchen

Merge branch 'next-1.x' into '1.x'

fix: rework snippet decoding

See merge request !25
Übergeordnete c1add9de 7d363ee0
No related branches found
Tags 1.1.0
1 Merge Request!25fix: rework snippet decoding
Pipeline #9310 mit Warnungen bestanden mit Phasen
in 1 Minute und 1 Sekunde
...@@ -110,7 +110,7 @@ define('package/quiqqer/html-snippets/bin/backend/Snippets', [ ...@@ -110,7 +110,7 @@ define('package/quiqqer/html-snippets/bin/backend/Snippets', [
buttons: [ buttons: [
{ {
name: 'addSnippet', name: 'addSnippet',
text: QUILocale.get('quiqqer/core', 'add'), text: QUILocale.get('quiqqer/quiqqer', 'add'),
position: 'left', position: 'left',
events: { events: {
click: this.add click: this.add
...@@ -118,7 +118,7 @@ define('package/quiqqer/html-snippets/bin/backend/Snippets', [ ...@@ -118,7 +118,7 @@ define('package/quiqqer/html-snippets/bin/backend/Snippets', [
}, { }, {
name: 'remove', name: 'remove',
icon: 'fa fa-trash', icon: 'fa fa-trash',
title: QUILocale.get('quiqqer/core', 'remove'), title: QUILocale.get('quiqqer/quiqqer', 'remove'),
disabled: true, disabled: true,
styles: { styles: {
'float': 'right' 'float': 'right'
......
(function() { (function() {
'use strict'; 'use strict';
const loadNode = function(Node) { const processSnippetNodes = function(nodes) {
if (Node.getAttribute('data-qui-html-snippet-gdpr-loaded')) { // Gather all HTML snippet nodes ("template" elements with a specific "data-" attribute)
return; const SnippetsNodeList = document.querySelectorAll('template[data-qui-html-snippet="gdpr"]');
}
Node.innerHTML = atob(Node.innerHTML); SnippetsNodeList.forEach((SnippetNode) => {
const scripts = Array.from(Node.getElementsByTagName('script')); const gdprCategory = SnippetNode.getAttribute('data-qui-html-snippet-gdpr-category');
scripts.forEach((script) => { // Only decode (and execute) the snippet node, if the corresponding gdpr category was accepted
const newScript = document.createElement('script'); window.GDPR.waitForCookieCategoryAcceptance(gdprCategory).then(() => {
newScript.textContent = script.textContent; decodeSnippetNode(SnippetNode);
});
});
};
const decodeSnippetNode = function(SnippetNode) {
// The node's inner HTML is base64 encoded, turn it back to normal HTML
SnippetNode.innerHTML = atob(SnippetNode.innerHTML);
// The snippet code is in a wrapping <template> node and thus isn't interpreted
// Therefore we have to move all nodes inside this template snippet node into the document
// We do that by iterating over all direct children of the template element and...
Array.from(SnippetNode.content.children).forEach(ChildNode => {
let NodeToInsert = ChildNode;
let i, len, attribute; // ...if it is a script tag it needs special treatment
if (ChildNode.tagName === 'SCRIPT') {
// "script" tags are not executed when moved from the snippet node to the document
// Therefore a new element has to be created instead
// The new element is executed when added to the document
NodeToInsert = createExecutableCopyOfScriptNode(ChildNode);
for (i = 0, len = script.attributes.length; i < len; i++) { // Remove the original script node as we are using the copy now
attribute = script.attributes[i]; ChildNode.remove();
newScript.setAttribute(attribute.name, attribute.value);
} }
script.parentNode.replaceChild(newScript, script); // ...moving the child node from inside the template snippet node in front of the snippet node
SnippetNode.insertAdjacentElement('beforebegin', NodeToInsert);
}); });
Node.setAttribute('data-qui-html-snippet-gdpr-loaded', 1); // ...and deleting the now empty snippet node
Node.style.display = ''; SnippetNode.remove();
}; };
const fetchNodes = function(nodes) { const createExecutableCopyOfScriptNode = function (ScriptNode) {
nodes.forEach((Node) => { const ExecutableScriptNode = document.createElement('script');
const gdprCategory = Node.getAttribute('data-qui-html-snippet-gdpr-category');
if (window.GDPR.isCookieCategoryAccepted(gdprCategory)) { // Copy the script's text content (the JavaScript code)
loadNode(Node); ExecutableScriptNode.textContent = ScriptNode.textContent;
} else {
window.GDPR.waitForCookieCategoryAcceptance(gdprCategory).then(() => { // Copy all the script's attributes
loadNode(Node); Array.from(ScriptNode.attributes).forEach(ScriptAttribute => {
}); ExecutableScriptNode.setAttribute(ScriptAttribute.name, ScriptAttribute.value);
}
}); });
return ExecutableScriptNode;
}; };
fetchNodes(document.querySelectorAll('[data-qui-html-snippet="gdpr"]')); processSnippetNodes();
window.whenQuiLoaded().then(() => { window.whenQuiLoaded().then(processSnippetNodes);
fetchNodes(document.querySelectorAll('[data-qui-html-snippet="gdpr"]'));
});
})(); })();
...@@ -67,17 +67,19 @@ class SnippetTemplateEvent ...@@ -67,17 +67,19 @@ class SnippetTemplateEvent
continue; continue;
} }
// consider gdpr status $snippetGdprCategory = $snippet['gdpr'];
$div = '<script '; $snippetContentEncoded = base64_encode($snippet['snippet']);
$div .= ' data-qui-html-snippet="gdpr"';
$div .= ' data-qui-html-snippet-gdpr-category="' . $snippet['gdpr'] . '"'; $snippetHtml = <<<EOF
$div .= ' style="display: none"'; <template
$div .= ' type="text/plain"'; data-qui-html-snippet="gdpr"
$div .= '>'; data-qui-html-snippet-gdpr-category="$snippetGdprCategory"
$div .= base64_encode($snippet['snippet']); >
$div .= '</script>'; $snippetContentEncoded
</template>
$Collector->append($div); EOF;
$Collector->append($snippetHtml);
} }
} }
} }
0% oder .
You are about to add 0 people to the discussion. Proceed with caution.
Bearbeitung dieser Nachricht zuerst beenden!
Bitte registrieren oder zum Kommentieren