diff --git a/lib/plugins/validators/sync/06_autoplay.js b/lib/plugins/validators/sync/06_autoplay.js index 2c8880c9d..1652b453f 100644 --- a/lib/plugins/validators/sync/06_autoplay.js +++ b/lib/plugins/validators/sync/06_autoplay.js @@ -23,60 +23,58 @@ export default { // create autoplay variant of the player if requested via link.autoplay attribute if (link.href && link.rel.indexOf(CONFIG.R.player) > -1 && link.autoplay && typeof link.autoplay === "string" && link.autoplay.indexOf('=') > -1) { // it should always be a query-string - var play = link.autoplay; - // don't need this field any longer + const play_querystring = link.autoplay; // e.g. 'autoplay=1' delete link.autoplay; - - if (link.href.indexOf(play) > -1 || link.rel.indexOf(CONFIG.R.autoplay) == -1) { - - var stop = play.replace(/\=(\w+)/, function (p1, p2) { - var antonyms = { - '1': '0', - '0': '1', - 'true': 'false', - 'false': 'true', - 'on': 'off', - 'off': 'on' - }; - return '=' + antonyms[p2]; - }); - - var rels = [...link.rel]; - var new_link = Object.assign({}, link); - new_link.rel = rels; // deep copy - - - if (link.href.indexOf(play) > -1 ) { - // original link is autoplay - if (link.rel.indexOf(CONFIG.R.autoplay) == -1 ) { - link.rel.push(CONFIG.R.autoplay); - } - - new_link.href = new_link.href.replace(play, stop); - if (new_link.rel.indexOf(CONFIG.R.autoplay) > -1 ) { - new_link.rel.splice(new_link.rel.indexOf(CONFIG.R.autoplay), 1); - } - - } else if (link.rel.indexOf(CONFIG.R.autoplay) == -1) { - - // original link isnt autoplay, leave it alone - // autoplay=false may autoplay. Many publishers react to any `autoplay` value - - if (link.href.indexOf(stop) > -1) { - new_link.href = new_link.href.replace(stop, play); - } else { - new_link.href += (new_link.href.indexOf('?') > -1 ? '&' : '?') + play; - } - - new_link.rel.push(CONFIG.R.autoplay); + const stop_querystring = play_querystring.replace(/\=(\w+)/, function (p1, p2) { + const antonyms = { + '1': '0', + '0': '1', + 'true': 'false', + 'false': 'true', + 'on': 'off', + 'off': 'on' + }; + return '=' + antonyms[p2]; + }); + + // Fix original link first, if needed + if (link.href.indexOf(play_querystring) > -1 && link.rel.indexOf(CONFIG.R.autoplay) === -1) { + link.rel.push(CONFIG.R.autoplay); + } + + if (link.href.indexOf(stop_querystring) > -1 && link.rel.indexOf(CONFIG.R.autoplay) > -1) { + link.rel.splice(link.rel.indexOf(CONFIG.R.autoplay), 1); + } + + var other = Object.assign({}, link); + other.rel = [...link.rel]; + + if (link.rel.indexOf(CONFIG.R.autoplay) === -1) { + + // Add a link with autoplay + if (link.href.indexOf(stop_querystring) > -1) { + other.href = other.href.replace(stop_querystring, play_querystring); + } else { + other.href += (other.href.indexOf('?') > -1 ? '&' : '?') + play_querystring; } + other.rel.push(CONFIG.R.autoplay); - return { - addLink: new_link - }; + } else { + + // Add a link without autoplay + if (link.href.indexOf(play_querystring) > -1) { + other.href = other.href.replace(play_querystring, stop_querystring); + } else { + other.href += (other.href.indexOf('?') > -1 ? '&' : '?') + stop_querystring; + } + other.rel.splice(other.rel.indexOf(CONFIG.R.autoplay), 1); } - + + return { + addLink: other + }; + } else if (link.autoplay) { delete link.autoplay; // if not a string - just ignore it }