Skip to content

Commit 6dd5ddb

Browse files
authored
Merge pull request #316 from bnookala/removing-uri-js
Removing URI.js
2 parents f82a288 + ef7f42f commit 6dd5ddb

File tree

2 files changed

+75
-33
lines changed

2 files changed

+75
-33
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
"redux": "^3.6.0",
3232
"redux-observable": "^0.13.0",
3333
"rxjs": "^5.0.3",
34-
"tslib": "^1.5.0",
35-
"urijs": "^1.18.4"
34+
"tslib": "^1.5.0"
3635
},
3736
"devDependencies": {
3837
"@types/chai": "^3.4.34",

src/Attachment.tsx

Lines changed: 74 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as React from 'react';
2-
import * as URI from 'urijs';
32

43
import { Attachment, Button } from 'botframework-directlinejs';
54
import { renderIfNonempty, konsole } from './Chat';
@@ -8,8 +7,50 @@ import { FormatState } from './Store';
87
const regExpCard = /\^application\/vnd\.microsoft\.card\./i;
98

109
const YOUTUBE_DOMAIN = "youtube.com";
10+
const YOUTUBE_WWW_DOMAIN = "www.youtube.com";
1111
const YOUTUBE_SHORT_DOMAIN = "youtu.be";
12+
const YOUTUBE_WWW_SHORT_DOMAIN = "www.youtu.be";
1213
const VIMEO_DOMAIN = "vimeo.com";
14+
const VIMEO_WWW_DOMAIN = "www.vimeo.com";
15+
16+
interface VideoEmbedQuery {
17+
loop?: number;
18+
autoplay?: number;
19+
modestbranding?: number;
20+
title?: number;
21+
byline?: number;
22+
portait?: number;
23+
badge?: number;
24+
[propName: string]: number;
25+
}
26+
27+
export const queryParams = (
28+
src: string
29+
) => {
30+
const queryObject = {};
31+
32+
src.substr(1)
33+
.split('&')
34+
.forEach(field => {
35+
const keyValue = field.split('=');
36+
queryObject[decodeURIComponent(keyValue[0])] = decodeURIComponent(keyValue[1]);
37+
});
38+
39+
return queryObject;
40+
}
41+
42+
const buildUrl = (
43+
src: string,
44+
query: VideoEmbedQuery,
45+
) => {
46+
return [
47+
src,
48+
'?',
49+
Object.keys(query)
50+
.map(key => encodeURIComponent(key) + '=' + encodeURIComponent(query[key].toString()))
51+
.join('&')
52+
].join('');
53+
}
1354

1455
const buttons = (
1556
buttons: Button[],
@@ -26,17 +67,15 @@ const Youtube = (props: {
2667
}) =>
2768
<iframe
2869
type="text/html"
29-
src={ new URI()
30-
.domain(YOUTUBE_DOMAIN)
31-
.subdomain("")
32-
.port("")
33-
.segment(["embed", props.embedId])
34-
.search({
35-
"modestbranding": 1,
36-
"loop": props.loop ? 1 : 0,
37-
"autoplay": props.autoPlay ? 1 : 0
38-
})
39-
.toString()
70+
src={
71+
buildUrl(
72+
`https://${YOUTUBE_DOMAIN}/embed/${props.embedId}`,
73+
{
74+
modestbranding: 1,
75+
loop: props.loop ? 1 : 0,
76+
autoplay: props.autoPlay ? 1 : 0
77+
}
78+
)
4079
}
4180
/>;
4281

@@ -47,20 +86,18 @@ const Vimeo = (props: {
4786
}) =>
4887
<iframe
4988
type="text/html"
50-
src={ new URI()
51-
.domain(VIMEO_DOMAIN)
52-
.subdomain("player")
53-
.port("")
54-
.segment(["video", props.embedId])
55-
.search({
56-
"title": 0,
57-
"byline": 0,
58-
"portrait": 0,
59-
"badge": 0,
60-
"autoplay": props.autoPlay ? 1 : 0,
61-
"loop": props.loop ? 1 : 0
62-
})
63-
.toString()
89+
src={
90+
buildUrl(
91+
`https://player.${VIMEO_DOMAIN}/video/${props.embedId}`,
92+
{
93+
title: 0,
94+
byline: 0,
95+
portrait: 0,
96+
badge: 0,
97+
autoplay: props.autoPlay ? 1 : 0,
98+
loop: props.loop ? 1 : 0
99+
}
100+
)
64101
}
65102
/>;
66103

@@ -72,19 +109,25 @@ const Video = (props: {
72109
onLoad?: () => void,
73110
onClick?: () => void,
74111
}) => {
75-
const src = new URI(props.src);
76-
const domain = src.domain();
112+
const url = document.createElement('a');
113+
url.href = props.src;
114+
115+
const urlQueryParams = queryParams(url.search);
116+
const pathSegments = url.pathname.substr(1).split('/');
77117

78-
switch (domain) {
118+
switch (url.hostname) {
79119
case YOUTUBE_DOMAIN:
80120
case YOUTUBE_SHORT_DOMAIN:
121+
case YOUTUBE_WWW_DOMAIN:
122+
case YOUTUBE_WWW_SHORT_DOMAIN:
81123
return <Youtube
82-
embedId={ domain === YOUTUBE_DOMAIN ? src.search(true).v : src.filename() }
124+
embedId={ url.hostname === YOUTUBE_DOMAIN || url.hostname === YOUTUBE_WWW_DOMAIN ? urlQueryParams['v'] : pathSegments[pathSegments.length-1] }
83125
{ ... props }
84126
/>;
85127

128+
case VIMEO_WWW_DOMAIN:
86129
case VIMEO_DOMAIN:
87-
return <Vimeo embedId={ src.filename() } { ...props } />
130+
return <Vimeo embedId={ pathSegments[pathSegments.length-1] } { ... props } />
88131

89132
default:
90133
return <video controls { ... props } />

0 commit comments

Comments
 (0)