Skip to content

Commit

Permalink
support *.videoji.hk domains as vimeo domains
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronm67 committed Apr 18, 2024
1 parent 8f37159 commit 30d2e89
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/lib/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import Player from '../player';
import { isVimeoUrl, isVimeoEmbed, getVimeoUrl } from './functions';
import { isVimeoUrl, isVimeoEmbed, getVimeoUrl, getOembedDomain } from './functions';
import { parseMessageData } from './postmessage';

const oEmbedParameters = [
Expand Down Expand Up @@ -108,7 +108,8 @@ export function getOEmbedData(videoUrl, params = {}, element) {
throw new TypeError(`“${videoUrl}” is not a vimeo.com url.`);
}

let url = `https://vimeo.com/api/oembed.json?url=${encodeURIComponent(videoUrl)}`;
const domain = getOembedDomain(videoUrl);
let url = `https://${domain}/api/oembed.json?url=${encodeURIComponent(videoUrl)}`;

for (const param in params) {
if (params.hasOwnProperty(param)) {
Expand Down
14 changes: 12 additions & 2 deletions src/lib/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function isInteger(value) {
* @return {boolean}
*/
export function isVimeoUrl(url) {
return (/^(https?:)?\/\/((player|www)\.)?vimeo\.com(?=$|\/)/).test(url);
return (/^(https?:)?\/\/((((player|www)\.)?vimeo\.com)|((player\.)?[a-zA-Z0-9-]+\.videoji\.hk))(?=$|\/)/).test(url);
}

/**
Expand All @@ -68,10 +68,20 @@ export function isVimeoUrl(url) {
* @return {boolean}
*/
export function isVimeoEmbed(url) {
const expr = /^https:\/\/player\.vimeo\.com\/video\/\d+/;
const expr = /^https:\/\/player\.((vimeo\.com)|([a-zA-Z0-9-]+\.videoji\.hk))\/video\/\d+/;
return expr.test(url);
}

export function getOembedDomain(url) {
const domain = (new URL(url).hostname ?? '').replace('player.', '');

Check failure on line 76 in src/lib/functions.js

View workflow job for this annotation

GitHub Actions / run

URL is not supported in op_mob 12.1, op_mini all, IE Mobile 10, IE 11, bb 7

if (domain.endsWith('.videoji.hk')) {
return domain;
}

return 'vimeo.com';
}

/**
* Get the Vimeo URL from an element.
* The element must have either a data-vimeo-id or data-vimeo-url attribute.
Expand Down
18 changes: 17 additions & 1 deletion test/functions-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';
import html from './helpers/html';
import { getMethodName, isDomElement, isInteger, isVimeoUrl, isVimeoEmbed, getVimeoUrl } from '../src/lib/functions';
import { getMethodName, isDomElement, isInteger, isVimeoUrl, isVimeoEmbed, getVimeoUrl, getOembedDomain } from '../src/lib/functions';

test('getMethodName properly formats the method name', (t) => {
t.true(getMethodName('color', 'get') === 'getColor');
Expand Down Expand Up @@ -40,6 +40,11 @@ test('isVimeoUrl identifies *.vimeo.com only', (t) => {
t.true(isVimeoUrl('https://www.vimeo.com') === true);
t.true(isVimeoUrl('//www.vimeo.com') === true);
t.true(isVimeoUrl('http://player.vimeo.com') === true);
t.true(isVimeoUrl('http://player.subdomain.videoji.hk') === true);
t.true(isVimeoUrl('http://player.subdomain.videoji.hk/video/12345') === true);
t.true(isVimeoUrl('http://player.subdomain.videoji.hk/video/12345?h=a1b2c3d4') === true);
t.true(isVimeoUrl('http://subdomain.videoji.hk') === true);
t.true(isVimeoUrl('http://videoji.hk') === false);
t.true(isVimeoUrl('//player.vimeo.com') === true);
t.true(isVimeoUrl('https://player.vimeo.com') === true);
t.true(isVimeoUrl('https://notvimeo.com') === false);
Expand All @@ -56,9 +61,20 @@ test('isVimeoEmbed identifies Vimeo embeds only', (t) => {
t.true(isVimeoEmbed('https://player.vimeo.com/video/76979871?h=8272103f6e') === true);
t.true(isVimeoEmbed('https://player.vimeo.com/video/76979871') === true);
t.true(isVimeoEmbed('http://player.vimeo.com/video/76979871?h=8272103f6e') === false);
t.true(isVimeoEmbed('http://player.subdomain.videoji.hk/video/76979871?h=8272103f6e') === false);
t.true(isVimeoEmbed('https://player.subdomain.videoji.hk/video/76979871?h=8272103f6e') === true);
t.true(isVimeoEmbed('http2://not-vimeo.com/video/76979871') === false);
});

test('getOembedDomain correctly returns a domain from a url', (t) => {
t.true(getOembedDomain('https://player.vimeo.com/video/76979871?h=8272103f6e') === 'vimeo.com');
t.true(getOembedDomain('https://player.vimeo.com/video/76979871') === 'vimeo.com');
t.true(getOembedDomain('http://player.vimeo.com/video/76979871?h=8272103f6e') === 'vimeo.com');
t.true(getOembedDomain('http://player.subdomain.videoji.hk/video/76979871?h=8272103f6e') === 'subdomain.videoji.hk');
t.true(getOembedDomain('https://player.subdomain.videoji.hk/video/76979871?h=8272103f6e') === 'subdomain.videoji.hk');
t.true(getOembedDomain('http2://not-vimeo.com/video/76979871') === 'vimeo.com');
})

test('getVimeoUrl correctly returns a url from the embed parameters', (t) => {
t.true(getVimeoUrl({ id: 445351154 }) === 'https://vimeo.com/445351154');
t.true(getVimeoUrl({ url: 'http://vimeo.com/445351154' }) === 'https://vimeo.com/445351154');
Expand Down

0 comments on commit 30d2e89

Please sign in to comment.