Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the script CSP friendly #468

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/js/core/touch-navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export default function touchNavigation(instance) {
lastZoomedPosX = null;
zoomedPosX = null;
zoomedPosY = null;
mediaImage.setAttribute('style', '');
mediaImage.style = '';
return;
}
if (scale > maxScale) {
Expand Down
8 changes: 5 additions & 3 deletions src/js/core/zoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export default class ZoomImages {
this.xOffset = 0;
this.yOffset = 0;

this.imgStylesMapper = new Map();

this.img.addEventListener('mousedown', (e) => this.dragStart(e), false);
this.img.addEventListener('mouseup', (e) => this.dragEnd(e), false);
this.img.addEventListener('mousemove', (e) => this.drag(e), false);
Expand Down Expand Up @@ -59,7 +61,7 @@ export default class ZoomImages {
}

const img = this.img;
img.setAttribute('data-style', img.getAttribute('style'));
this.imgStylesMapper.set(img, img.style);
img.style.maxWidth = img.naturalWidth + 'px';
img.style.maxHeight = img.naturalHeight + 'px';

Expand All @@ -71,8 +73,8 @@ export default class ZoomImages {
this.zoomedIn = true;
}
zoomOut() {
this.img.parentNode.setAttribute('style', '');
this.img.setAttribute('style', this.img.getAttribute('data-style'));
this.img.parentNode.style = '';
this.img.style = this.imgStylesMapper.get(this.img) || '';
this.slide.classList.remove('zoomed');
this.zoomedIn = false;
this.currentX = null;
Expand Down
23 changes: 8 additions & 15 deletions src/js/glightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,8 @@ class GlightboxInit {

const scrollBar = window.innerWidth - document.documentElement.clientWidth;
if (scrollBar > 0) {
var styleSheet = document.createElement('style');
styleSheet.type = 'text/css';
styleSheet.className = 'gcss-styles';
styleSheet.innerText = `.gscrollbar-fixer {margin-right: ${scrollBar}px}`;
document.head.appendChild(styleSheet);
_.addClass(body, 'gscrollbar-fixer');
document.body.style.marginRight = `${scrollBar}px`;
_.addClass(body, 'gscrollbar-fixer'); // backwards compatibility
}

_.addClass(body, 'glightbox-open');
Expand Down Expand Up @@ -1085,8 +1081,8 @@ class GlightboxInit {
let descHeight = description.offsetHeight;
let imgNode = image.querySelector('img');

imgNode.setAttribute('style', `max-height: calc(100vh - ${descHeight}px)`);
description.setAttribute('style', `max-width: ${imgNode.offsetWidth}px;`);
imgNode.style.maxHeight = `calc(100vh - ${descHeight}px)`;
description.style.maxWidth = `${imgNode.offsetWidth}px`;
}
}

Expand Down Expand Up @@ -1134,15 +1130,15 @@ class GlightboxInit {
let vheight = video.offsetHeight;
let ratio = winHeight / vheight;
let vsize = { width: vwidth * ratio, height: vheight * ratio };
video.parentNode.setAttribute('style', `max-width: ${vsize.width}px`);
video.parentNode.style.maxWidth = `${vsize.width}px`;

if (descriptionResize) {
description.setAttribute('style', `max-width: ${vsize.width}px;`);
description.style.maxWidth = `${vsize.width}px`;
}
} else {
video.parentNode.style.maxWidth = `${videoWidth}`;
if (descriptionResize) {
description.setAttribute('style', `max-width: ${videoWidth};`);
description.style.maxWidth = videoWidth;
}
}
}
Expand Down Expand Up @@ -1239,6 +1235,7 @@ class GlightboxInit {
const body = document.body;
_.removeClass(html, 'glightbox-open');
_.removeClass(body, 'glightbox-open touching gdesc-open glightbox-touch glightbox-mobile gscrollbar-fixer');
document.body.style.marginRight = '';
this.modal.parentNode.removeChild(this.modal);

this.trigger('close');
Expand All @@ -1248,10 +1245,6 @@ class GlightboxInit {
this.settings.onClose();
}

const styles = document.querySelector('.gcss-styles');
if (styles) {
styles.parentNode.removeChild(styles);
}
this.lightboxOpen = false;
this.closing = null;
});
Expand Down
54 changes: 37 additions & 17 deletions src/js/slides/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ export default function slideVideo(slide, data, index, callback) {

const videoWrapper = slide.querySelector('.gvideo-wrapper');

injectAssets(this.settings.plyr.css, 'Plyr');
if (typeof this.settings.plyr.css === 'function') {
this.settings.plyr.css();
} else {
injectAssets(this.settings.plyr.css, 'Plyr');
}

let url = data.href;
let provider = data?.videoProvider;
let customPlaceholder = false;

slideMedia.style.maxWidth = data.width;

injectAssets(this.settings.plyr.js, 'Plyr', () => {
const initPlyr = (Plyr, config) => {
// Set vimeo videos
if (!provider && url.match(/vimeo\.com\/([0-9]*)/)) {
provider = 'vimeo';
Expand All @@ -46,27 +50,37 @@ export default function slideVideo(slide, data, index, callback) {
// if no provider, default to local
if (provider === 'local' || !provider) {
provider = 'local';
let html = '<video id="' + videoID + '" ';
html += `style="background:#000; max-width: ${data.width};" `;
html += 'preload="metadata" ';
html += 'x-webkit-airplay="allow" ';
html += 'playsinline ';
html += 'controls ';
html += 'class="gvideo-local">';
html += `<source src="${url}">`;
html += '</video>';
customPlaceholder = createHTML(html);

const video = document.createElement('video');
video.id = videoID;
video.className = 'gvideo-local';
video.preload = 'metadata';
video.playsInline = true;
video.controls = true;
video.style.background = '#000';
video.style.maxWidth = data.width;
video.setAttribute('x-webkit-airplay', 'allow');

const source = document.createElement('source');
source.src = url;
video.appendChild(source);

customPlaceholder = video;
}

// prettier-ignore
const placeholder = customPlaceholder ? customPlaceholder : createHTML(`<div id="${videoID}" data-plyr-provider="${provider}" data-plyr-embed-id="${url}"></div>`);
if (!customPlaceholder) {
customPlaceholder = document.createElement('div');
customPlaceholder.id = videoID;
customPlaceholder.dataset.plyrProvider = provider;
customPlaceholder.dataset.plyrEmbedId = url;
}

addClass(videoWrapper, `${provider}-video gvideo`);
videoWrapper.appendChild(placeholder);
videoWrapper.appendChild(customPlaceholder);
videoWrapper.setAttribute('data-id', videoID);
videoWrapper.setAttribute('data-index', index);

const playerConfig = has(this.settings.plyr, 'config') ? this.settings.plyr.config : {};
const playerConfig = config || (has(this.settings.plyr, 'config') ? this.settings.plyr.config : {});
const player = new Plyr('#' + videoID, playerConfig);

player.on('ready', (event) => {
Expand All @@ -85,7 +99,13 @@ export default function slideVideo(slide, data, index, callback) {
);
player.on('enterfullscreen', handleMediaFullScreen);
player.on('exitfullscreen', handleMediaFullScreen);
});
}

if (typeof this.settings.plyr.js === 'function') {
this.settings.plyr.js().then(v => initPlyr(v.Plyr, v.config));
} else {
injectAssets(this.settings.plyr.js, 'Plyr', () => initPlyr(window.Plyr));
}
}

/**
Expand Down