Add link to extract titles from WebM metadata.

This commit is contained in:
ccd0 2015-02-02 02:49:28 -08:00
parent 7ab7b178ff
commit 9b868b760b
5 changed files with 119 additions and 40 deletions

View File

@ -205,6 +205,10 @@ Config =
true true
'Add sauce links to images.' 'Add sauce links to images.'
] ]
'WEBM Metadata': [
true
'Add link to fetch title metadata from webm videos.'
]
'Reveal Spoiler Thumbnails': [ 'Reveal Spoiler Thumbnails': [
false false
'Replace spoiler thumbnails with the original image.' 'Replace spoiler thumbnails with the original image.'

View File

@ -10,40 +10,31 @@ CrossOrigin = do ->
callbacks[id] = cb callbacks[id] = cb
<% } %> <% } %>
file: do -> binary: (url, cb, headers={}) ->
makeBlob = (urlBlob, contentType, contentDisposition, url) ->
name = url.match(/([^\/]+)\/*$/)?[1]
mime = contentType?.match(/[^;]*/)[0] or 'application/octet-stream'
match =
contentDisposition?.match(/\bfilename\s*=\s*"((\\"|[^"])+)"/i)?[1] or
contentType?.match(/\bname\s*=\s*"((\\"|[^"])+)"/i)?[1]
if match
name = match.replace /\\"/g, '"'
blob = new Blob([urlBlob], {type: mime})
blob.name = name
blob
(url, cb) ->
<% if (type === 'crx') { %> <% if (type === 'crx') { %>
if /^https:\/\//.test(url) or location.protocol is 'http:' if /^https:\/\//.test(url) or location.protocol is 'http:'
$.ajax url, xhr = new XMLHttpRequest()
responseType: 'blob' xhr.open 'GET', url, true
onload: -> xhr.setRequestHeader key, value for key, value of headers
return cb null unless @readyState is @DONE and @status is 200 xhr.responseType = 'arraybuffer'
xhr.onload = ->
return cb null unless @readyState is @DONE and @status in [200, 206]
contentType = @getResponseHeader 'Content-Type' contentType = @getResponseHeader 'Content-Type'
contentDisposition = @getResponseHeader 'Content-Disposition' contentDisposition = @getResponseHeader 'Content-Disposition'
cb (makeBlob @response, contentType, contentDisposition, url) cb new Uint8Array(@response), contentType, contentDisposition
onerror: -> xhr.onerror = xhr.onabort = ->
cb null cb null
xhr.send()
else else
eventPageRequest url, 'arraybuffer', ({response, contentType, contentDisposition, error}) -> eventPageRequest url, 'arraybuffer', ({response, contentType, contentDisposition, error}) ->
return cb null if error return cb null if error
cb (makeBlob new Uint8Array(response), contentType, contentDisposition, url) cb new Uint8Array(response), contentType, contentDisposition
<% } %> <% } %>
<% if (type === 'userscript') { %> <% if (type === 'userscript') { %>
GM_xmlhttpRequest GM_xmlhttpRequest
method: "GET" method: "GET"
url: url url: url
headers: headers
overrideMimeType: "text/plain; charset=x-user-defined" overrideMimeType: "text/plain; charset=x-user-defined"
onload: (xhr) -> onload: (xhr) ->
r = xhr.responseText r = xhr.responseText
@ -54,11 +45,27 @@ CrossOrigin = do ->
i++ i++
contentType = xhr.responseHeaders.match(/Content-Type:\s*(.*)/i)?[1] contentType = xhr.responseHeaders.match(/Content-Type:\s*(.*)/i)?[1]
contentDisposition = xhr.responseHeaders.match(/Content-Disposition:\s*(.*)/i)?[1] contentDisposition = xhr.responseHeaders.match(/Content-Disposition:\s*(.*)/i)?[1]
cb (makeBlob data, contentType, contentDisposition, url) cb data, contentType, contentDisposition
onerror: -> onerror: ->
cb null cb null
onabort: ->
cb null
<% } %> <% } %>
file: (url, cb) ->
CrossOrigin.binary url, (data, contentType, contentDisposition) ->
return cb null unless data?
name = url.match(/([^\/]+)\/*$/)?[1]
mime = contentType?.match(/[^;]*/)[0] or 'application/octet-stream'
match =
contentDisposition?.match(/\bfilename\s*=\s*"((\\"|[^"])+)"/i)?[1] or
contentType?.match(/\bname\s*=\s*"((\\"|[^"])+)"/i)?[1]
if match
name = match.replace /\\"/g, '"'
blob = new Blob([data], {type: mime})
blob.name = name
cb blob
json: do -> json: do ->
callbacks = {} callbacks = {}
responses = {} responses = {}

View File

@ -342,6 +342,7 @@ Main =
['Image Loading', ImageLoader] ['Image Loading', ImageLoader]
['Image Hover', ImageHover] ['Image Hover', ImageHover]
['Volume Control', Volume] ['Volume Control', Volume]
['WEBM Metadata', Metadata]
['Comment Expansion', ExpandComment] ['Comment Expansion', ExpandComment]
['Thread Expansion', ExpandThread] ['Thread Expansion', ExpandThread]
['Thread Excerpt', ThreadExcerpt] ['Thread Excerpt', ThreadExcerpt]

View File

@ -928,6 +928,9 @@ span.hide-announcement {
.fileThumb > .warning { .fileThumb > .warning {
clear: both; clear: both;
} }
a.webm-title.ready {
text-decoration: underline;
}
input[name="Default Volume"] { input[name="Default Volume"] {
width: 4em; width: 4em;
height: 1ex; height: 1ex;

View File

@ -0,0 +1,64 @@
Metadata =
init: ->
return unless Conf['WEBM Metadata'] and g.VIEW in ['index', 'thread'] and g.BOARD.ID isnt 'f'
Post.callbacks.push
name: 'WEBM Metadata'
cb: @node
node: ->
return unless @file and /webm$/i.test @file.URL
if @isClone
link = $ '.webm-title', @file.text
else
link = $.el 'a',
className: 'webm-title ready'
href: 'javascript:;'
textContent: 'title'
$.add @file.text, [$.tn('\u00A0'), link]
$.on link, 'click', Metadata[if link.dataset.title? then 'toggle' else 'load']
load: ->
$.off @, 'click', Metadata.load
$.rmClass @, 'ready'
@textContent = '...'
CrossOrigin.binary Get.postFromNode(@).file.URL, (data) =>
if data?
Metadata.parse.call @, data
$.on @, 'click', Metadata.toggle
else
@textContent = 'error'
$.on @, 'click', Metadata.load
,
Range: 'bytes=0-9999'
parse: (data) ->
readInt = ->
n = data[i++]
len = 0
len++ while n < (0x80 >> len)
n ^= (0x80 >> len)
while len-- and i < data.length
n = (n << 8) ^ data[i++]
n
i = 0
while i < data.length
element = readInt()
size = readInt()
if element is 0x3BA9 # Title
title = ''
while size-- and i < data.length
title += String.fromCharCode data[i++]
@textContent = @dataset.title = decodeURIComponent escape title # UTF-8 decoding
return
else unless element in [0x8538067, 0x549A966] # Segment, Info
i += size
@textContent = 'not found'
toggle: ->
@textContent = if $.hasClass @, 'ready'
@dataset.title or 'not found'
else
'title'
$.toggleClass @, 'ready'