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

support *.videoji.hk domains as vimeo domains #1026

Merged
merged 1 commit into from
Apr 24, 2024
Merged
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
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
15 changes: 13 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,21 @@ 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 match = (url || '').match(/^(?:https?:)?(?:\/\/)?([^/?]+)/);
const domain = ((match && match[1]) || '').replace('player.', '');

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
21 changes: 20 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,23 @@ 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');
t.true(getOembedDomain(null) === 'vimeo.com');
t.true(getOembedDomain(undefined) === 'vimeo.com');
t.true(getOembedDomain('') === '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
Loading