Skip to content

Commit 14e93fd

Browse files
authored
Merge pull request #1652 from hackmdio/bugfix/fix-vimeo-jsonp-callback
fix: vimeo won't show up due to the jsonp callback data unable be parsed with jQuery
2 parents c1a1290 + 2b9affb commit 14e93fd

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

public/js/extra.js

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,23 @@ if (typeof window.mermaid !== 'undefined' && window.mermaid) {
260260
}
261261
}
262262

263+
function jsonp (url, callback) {
264+
const callbackName = 'jsonp_callback_' + Math.round(1000000000 * Math.random())
265+
window[callbackName] = function (data) {
266+
delete window[callbackName]
267+
document.body.removeChild(script)
268+
callback(data)
269+
}
270+
271+
const script = document.createElement('script')
272+
script.src = url + (url.indexOf('?') >= 0 ? '&' : '?') + 'callback=' + callbackName
273+
document.body.appendChild(script)
274+
script.onerror = function (e) {
275+
console.error(e)
276+
script.remove()
277+
}
278+
}
279+
263280
// dynamic event or object binding here
264281
export function finishView (view) {
265282
// todo list
@@ -304,17 +321,11 @@ export function finishView (view) {
304321
imgPlayiframe(this, '//player.vimeo.com/video/')
305322
})
306323
.each((key, value) => {
307-
$.ajax({
308-
type: 'GET',
309-
url: `//vimeo.com/api/v2/video/${$(value).attr('data-videoid')}.json`,
310-
jsonp: 'callback',
311-
dataType: 'jsonp',
312-
success (data) {
313-
const thumbnailSrc = data[0].thumbnail_large
314-
const image = `<img src="${thumbnailSrc}" />`
315-
$(value).prepend(image)
316-
if (window.viewAjaxCallback) window.viewAjaxCallback()
317-
}
324+
jsonp(`//vimeo.com/api/v2/video/${$(value).attr('data-videoid')}.json`, function (data) {
325+
const thumbnailSrc = data[0].thumbnail_large
326+
const image = `<img src="${thumbnailSrc}" />`
327+
$(value).prepend(image)
328+
if (window.viewAjaxCallback) window.viewAjaxCallback()
318329
})
319330
})
320331
// gist

0 commit comments

Comments
 (0)