Skip to content

Commit

Permalink
domains: fix private Soundclouds (#560)
Browse files Browse the repository at this point in the history
by adding required oEmbed iframe `assignQuerystring` method that is used in main Soundcloud plugin
  • Loading branch information
iparamonau authored Oct 2, 2024
1 parent ad45915 commit bab08d0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 42 deletions.
83 changes: 47 additions & 36 deletions lib/plugins/system/oembed/oembed.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,51 +31,60 @@ function fixOembedIframeAttributes(obj) {
}

function _getOembedIframe(oembed) {
if (!oembed.html) {
return null;
}

if (typeof oembed._iframe === 'undefined') {

// Allow encoded entities if they start from $lt;
var html = oembed.html5 || oembed.html;
if (/^<$/i.test(html)) {
html = entities.decodeHTML(html);
}
var _iframe = null;

if (oembed.html5 || oembed.html) {

// Allow encoded entities if they start from $lt;
var html = oembed.html5 || oembed.html;
if (/^<$/i.test(html)) {
html = entities.decodeHTML(html);
}

var $container = cheerio('<div>');
try {
$container.html(html);
} catch (ex) {}
var $iframe = $container.find('iframe');

if ($iframe.length === 2 && /<iframe>$/i.test(html)) {
// Forgive mis-closed iFrame tag
$iframe = $iframe.first();
}

var $container = cheerio('<div>');
try {
$container.html(html);
} catch (ex) {}
var $iframe = $container.find('iframe');
if ($iframe.length === 1) {
_iframe = fixOembedIframeAttributes($iframe[0].attribs);
_iframe.placeholder = oembed.thumbnail_url;
}

if ($iframe.length === 2 && /<iframe>$/i.test(html)) {
// Forgive mis-closed iFrame tag
$iframe = $iframe.first();
// When oembed is in fact iframe from domain fallbacks on oEmbedError
} else if (oembed.src) {
_iframe = oembed;
}

if ($iframe.length === 1) {
oembed._iframe = fixOembedIframeAttributes($iframe[0].attribs);

if (oembed._iframe && oembed._iframe.src) {
var src = URL.parse(oembed._iframe.src, true);
oembed._iframe.host = src.host;
oembed._iframe.pathname = src.pathname;
oembed._iframe.path = src.path;
oembed._iframe.query = src.query;
oembed._iframe.placeholder = oembed.thumbnail_url;
oembed._iframe.replaceQuerystring = function(params) {
var qs = querystring.stringify({...oembed._iframe.query, ...params});
return oembed._iframe.src.replace(/\?.*$/, '') + (qs ? '?' + qs : '');
}
oembed._iframe.assignQuerystring = function(params) {
var qs = querystring.stringify(params);
return oembed._iframe.src.replace(/\?.*$/, '') + (qs ? '?' + qs : '');
}
if (_iframe && _iframe.src) {
var src = URL.parse(_iframe.src, true);
_iframe.host = src.host;
_iframe.pathname = src.pathname;
_iframe.path = src.path;
_iframe.query = src.query;
_iframe.replaceQuerystring = function(params) {
var qs = querystring.stringify({..._iframe.query, ...params});
return _iframe.src.replace(/\?.*$/, '') + (qs ? '?' + qs : '');
}
_iframe.assignQuerystring = function(params) {
var qs = querystring.stringify(params);
return oembed._iframe.src.replace(/\?.*$/, '') + (qs ? '?' + qs : '');
}
} else {
oembed._iframe = null;
_iframe = null;
}
}

oembed._iframe = {..._iframe};
}

return oembed._iframe;
}
Expand All @@ -99,6 +108,8 @@ export default {

provides: ['self', 'oembedError'],

getIframe: _getOembedIframe, // available via export for fallbacks as plugins['oembed'].getIframe(obj)

getData: function(url, oembedLinks, options, cb) {

var href = oembedLinks[0].href;
Expand Down
13 changes: 7 additions & 6 deletions plugins/domains/soundcloud.com/soundcloud-oembed-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ export default {

provides: ['__allow_soundcloud_meta', 'iframe'],

getData: function(oembedError, twitter, options, cb) {
if (oembedError === 403 && !options.getProviderOptions('soundcloud.disable_private', false) && twitter.player) {
getData: function(oembedError, twitter, options, plugins, cb) {
var disable_private = options.getProviderOptions('soundcloud.disable_private', false)
if (oembedError === 403 && !disable_private && twitter.player) {
return cb(null, {
__allow_soundcloud_meta: true,
iframe: {
src: twitter.player.value,
iframe: plugins['oembed'].getIframe({
src: twitter.player.value?.replace('origin=twitter', 'origin=iframely'),
height: twitter.player.height
},
}),
message: "Contact support to disable private Soundcloud audio."
});
} else if (oembedError === 403 && options.getProviderOptions('soundcloud.disable_private', false)) {
} else if (oembedError === 403 && disable_private) {
return cb({
responseError: oembedError
});
Expand Down

0 comments on commit bab08d0

Please sign in to comment.