Skip to content

Commit

Permalink
nit: Minor code cleanup
Browse files Browse the repository at this point in the history
- Only add privacy url to evl when consent is required
- Change flow in videolink.js
- Catch possible json exception in getIframeConfig
  • Loading branch information
octfx committed Oct 17, 2023
1 parent c42adcf commit 979bacf
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 35 deletions.
6 changes: 6 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.storybook/
/docs/
/i18n/
/node_modules/
/vendor/
Gruntfile.js
20 changes: 20 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"root": true,
"extends": [
"wikimedia/client",
"wikimedia/jquery",
"wikimedia/mediawiki"
],
"globals": {
"require": "readonly",
"module": "readonly"
},
"rules": {
"one-var": "off",
"//": [
"off",
"ResourceLoader's `packageFiles` do not require wrapping but the `module` option is only available in ES6+."
],
"no-implicit-globals": "off"
}
}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
.DS_Store
.svn
/vendor
composer.lock
/composer.lock
/node_modules
/.eslintcache
/package-lock.json
9 changes: 6 additions & 3 deletions includes/EmbedService/AbstractEmbedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,8 @@ public function getTitle(): ?string {
* @param null|int $width
* @param null|int $height
* @return string
* @throws JsonException
*/
public function getIframeConfig( ?int $width = 0, ?int $height = 0 ): string {
public function getIframeConfig( $width = 0, $height = 0 ): string {
$attributes = [];
if ( !empty( $width ) && $width !== $this->getDefaultWidth() ) {
$attributes['width'] = $width;
Expand All @@ -498,7 +497,11 @@ public function getIframeConfig( ?int $width = 0, ?int $height = 0 ): string {

$attributes['src'] = $this->getUrl();

return json_encode( $attributes, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES );
try {
return json_encode( $attributes, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES );
} catch ( JsonException $e ) {
return '{"error": "Could not encode iframe config"}';
}
}

/**
Expand Down
7 changes: 5 additions & 2 deletions includes/EmbedVideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,15 @@ public static function parseEVL( Parser $parser, PPFrame $frame, array $args ):
$linkConfig = [
'data-iframeconfig' => $ev->service->getIframeConfig( $ev->args['width'], $ev->args['height'] ),
'data-service' => $ev->args['service'],
'data-privacy-url' => $ev->service->getPrivacyPolicyUrl(),
'data-player' => $ev->args['player'] ?? 'default',
'class' => 'embedvideo-evl vplink',
'href' => '#',
];

if ( MediaWikiServices::getInstance()->getMainConfig()->get( 'EmbedVideoRequireConsent' ) === true ) {
$linkConfig['data-privacy-url'] = $ev->service->getPrivacyPolicyUrl();
}

return [
Html::element( 'a', $linkConfig, $ev->args['text'] ),
'noparse' => true,
Expand Down Expand Up @@ -366,7 +369,7 @@ private function parseArgs( array $args, bool $fromTag ): array {
if ( ( $keys[$counter] !== 'urlArgs' || str_contains( $arg, 'urlArgs' ) ) && preg_match( '/https?:/', $arg ) !== 1 ) {
$pair = explode( '=', $arg, 2 );
}
$pair = array_map( 'trim', $pair );
$pair = array_map( 'strip_tags', array_map( 'trim', $pair ) );

// We are handling a named argument
if ( count( $pair ) === 2 ) {
Expand Down
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"private": true,
"scripts": {
"lint:fix:js": "npm -s run lint:js -- --fix",
"lint:js": "eslint --cache --max-warnings 0 ."
},
"devDependencies": {
"grunt": "1.3.0",
"grunt-banana-checker": "0.9.0",
"eslint-config-wikimedia": "0.17.0",
"jsdoc": "3.6.3",
"jsdoc-wmf-theme": "0.0.3",
"stylelint-config-idiomatic-order": "8.1.0",
"stylelint-config-wikimedia": "0.10.3"
}
}
2 changes: 1 addition & 1 deletion resources/ext.embedVideo.consent.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const makeIframe = require('./iframe.js').makeIframe;
const { makeIframe } = require('./iframe.js');

(function () {
mw.hook( 'wikipage.content' ).add( () => {
Expand Down
55 changes: 29 additions & 26 deletions resources/ext.embedVideo.videolink.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,57 @@ const {makeIframe, fetchThumb} = require('./iframe.js');
document.querySelectorAll('.embedvideo-evl').forEach(function (evl) {
evl.addEventListener('click', e => {
e.preventDefault();
e.stopPropagation();

const player = evl?.dataset?.player ?? 'default';
const iframeConfig = JSON.parse(evl.dataset.iframeconfig);

const iframe = document.querySelector(`.embedvideo.evlplayer-${player} iframe`);
// Iframe exists, no consent required or already given
if (iframe !== null) {
for (const [key, value] of Object.entries(iframeConfig)) {
iframe.setAttribute(key, value);
}
} else {
const div = document.querySelector(`.embedvideo.evlplayer-${player}`);

if (div === null) {
console.warn(`No player with id '${player}' found!.`);
return;
}
return;
}

// No iframe exists, only when explicit consent is required
const div = document.querySelector(`.embedvideo.evlplayer-${player}`);

if (div === null || evl.dataset?.iframeconfig === null) {
console.warn(`No player with id '${player}' found!.`);
return;
}

const wrapper = div.querySelector('.embedvideo-wrapper');
const consentDiv = wrapper.querySelector('.embedvideo-consent');
const wrapper = div.querySelector('.embedvideo-wrapper');
const consentDiv = wrapper.querySelector('.embedvideo-consent');

const origService = div?.dataset?.service;
const origService = div.dataset?.service;

div.dataset.iframeconfig = evl.dataset.iframeconfig;
div.dataset.service = evl.dataset.service;
div.dataset.iframeconfig = evl.dataset.iframeconfig;
div.dataset.service = evl.dataset.service;

const message = 'embedvideo-service-' + evl.dataset.service;
const service = mw.message(message).escaped();
const serviceMessage = mw.message('embedvideo-service-' + (evl.dataset?.service ?? 'youtube')).escaped();
const privacyMessage = mw.message('embedvideo-consent-privacy-notice-text', serviceMessage).escaped();

div.querySelector('.embedvideo-loader__service').innerText = serviceMessage;
div.querySelector('.embedvideo-privacyNotice__content').innerText = privacyMessage;

if (evl.dataset?.privacyUrl !== null) {
const link = document.createElement('a');
link.href = evl?.dataset?.privacyUrl ?? '#';
link.href = evl.dataset.privacyUrl;
link.rel = 'nofollow,noopener';
link.target = '_blank';
link.classList.add('embedvideo-privacyNotice__link');
link.innerText = mw.message('embedvideo-consent-privacy-policy').escaped();

div.querySelector('.embedvideo-loader__service').innerText = service;
div.querySelector('.embedvideo-privacyNotice__content').innerText = mw.message('embedvideo-consent-privacy-notice-text', service).escaped();

if (link.href !== '#') {
div.querySelector('.embedvideo-privacyNotice__content').appendChild(link);
}
div.querySelector('.embedvideo-privacyNotice__content').appendChild(link);
}

if (origService === 'videolink') {
makeIframe(div);
} else {
fetchThumb(iframeConfig.src, consentDiv, wrapper.parentElement);
}
if (origService === 'videolink') {
makeIframe(div);
} else {
fetchThumb(iframeConfig.src, consentDiv, wrapper.parentElement);
}
});
});
Expand Down
2 changes: 0 additions & 2 deletions resources/modules/iframe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

const fetchThumb = async (url, parent, container) => {
const fetcherFactory = require('./fetchFactory.js').fetchFactory;

Expand Down Expand Up @@ -119,7 +118,6 @@ const makeIframe = function(ev) {
let iframeConfig = ev.dataset.iframeconfig;

iframeConfig = {
...mw.config.get('ev-default-config') ?? [],
...mw.config.get('ev-' + ev.dataset.service + '-config') ?? [],
...JSON.parse(iframeConfig)
};
Expand Down

0 comments on commit 979bacf

Please sign in to comment.