Skip to content

Commit 73f7ff2

Browse files
committed
videolink: url time parsing logic working
1 parent 156b144 commit 73f7ff2

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

coffee/src/shells/youtube.shell.coffee

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ YouTubeShell = acorn.shells.YouTubeShell =
2121
]
2222

2323

24-
2524
class YouTubeShell.Model extends VideoLinkShell.Model
2625

2726

@@ -57,6 +56,19 @@ class YouTubeShell.Model extends VideoLinkShell.Model
5756

5857
pattern.exec(link)[3]
5958

59+
parseTime: (time) =>
60+
validTimePatterns = [
61+
/(\d+)/
62+
/(\d+)m(\d+)s/
63+
]
64+
65+
# the second one is more general, work backwards
66+
if validTimePatterns[1].test time
67+
return 60 * parseInt(validTimePatterns[1].exec(time)[1]) + parseInt(validTimePatterns[1].exec(time)[2])
68+
else if validTimePatterns[0].test time
69+
return parseInt(validTimePatterns[0].exec(time)[0])
70+
else
71+
return undefined
6072

6173
embedLink: (options) =>
6274
# see https://developers.google.com/youtube/player_parameters for options
@@ -102,6 +114,27 @@ class YouTubeShell.RemixView extends VideoLinkShell.RemixView
102114
@model.timeTotal data.data.duration
103115

104116
@model._updateAttributesWithDefaults()
117+
start = acorn.util.fetchParameters this.model.link(), ["t", "start"]
118+
# take the first possible valid parameter
119+
120+
start = @model.timeStart()
121+
unless _.isNumber start
122+
start = acorn.util.fetchParameters this.model.link(), ["t", "start"]
123+
start = @model.parseTime _.values(start)[0]
124+
unless _.isNumber start
125+
start = 0
126+
127+
end = @model.timeEnd()
128+
unless _.isNumber end
129+
end = acorn.util.fetchParameters this.model.link(), ["end"]
130+
end = @model.parseTime _.values(end)[0]
131+
unless _.isNumber end
132+
end = @model.timeTotal()
133+
134+
@model.timeStart(start)
135+
@model.timeEnd(end)
136+
137+
@_setTimeInputMin()
105138
@_setTimeInputMax()
106139

107140

coffee/src/util/util.coffee

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,26 @@ util.parseUrl = (url) ->
274274

275275
result
276276

277+
# function takes a URL and an array of parameters
278+
# returns JSON object with matching parameters and values, if any
279+
# otherwise, returns empty object
280+
# http://stackoverflow.com/a/8649003
281+
util.fetchParameters = (url, params) ->
282+
url = $.trim url
283+
url = "http://#{url}" unless /^([a-z0-9]+:)?\/\//i.test url
284+
anchor = document.createElement 'a'
285+
anchor.href = url
286+
287+
search = anchor.search.substring(1)
288+
search = '{"' + search.replace(/&/g, '","').replace(/\=/g, '":"') + '"}'
289+
290+
parameters = if search is "" then {} else JSON.parse search, (key, value) ->
291+
if key is "" then value else decodeURIComponent value
292+
277293

294+
console.log(parameters)
295+
_.pick parameters, params
296+
278297

279298
# track mouse location at all times
280299
util.mouseLocationTracker = (->

coffee/src/views/index.coffee

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
`import "shell_editor_view"`
2222
`import "shell_options_view"`
2323
`import "shell_selector_view"`
24-
`import "slider_handle_view"`
2524
`import "sliding_bar_view"`
2625
`import "sliding_object_view"`
2726
`import "sources_view"`

0 commit comments

Comments
 (0)