Skip to content

Commit 0e0ef55

Browse files
iparamonaunleush
authored andcommitted
[Domains] Fix Bunny CDN autoplay issues via Twitter Player domain flags (#612)
* [Domains] Fix Bunny CDN autoplay issues via Twitter Player domain flags * simplify code
1 parent e1591fc commit 0e0ef55

File tree

1 file changed

+47
-49
lines changed

1 file changed

+47
-49
lines changed

lib/plugins/validators/sync/06_autoplay.js

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,60 +23,58 @@ export default {
2323
// create autoplay variant of the player if requested via link.autoplay attribute
2424
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
2525

26-
var play = link.autoplay;
27-
// don't need this field any longer
26+
const play_querystring = link.autoplay; // e.g. 'autoplay=1'
2827
delete link.autoplay;
29-
30-
if (link.href.indexOf(play) > -1 || link.rel.indexOf(CONFIG.R.autoplay) == -1) {
31-
32-
var stop = play.replace(/\=(\w+)/, function (p1, p2) {
33-
var antonyms = {
34-
'1': '0',
35-
'0': '1',
36-
'true': 'false',
37-
'false': 'true',
38-
'on': 'off',
39-
'off': 'on'
40-
};
41-
return '=' + antonyms[p2];
42-
});
43-
44-
var rels = [...link.rel];
45-
var new_link = Object.assign({}, link);
46-
new_link.rel = rels; // deep copy
47-
48-
49-
if (link.href.indexOf(play) > -1 ) {
50-
// original link is autoplay
51-
if (link.rel.indexOf(CONFIG.R.autoplay) == -1 ) {
52-
link.rel.push(CONFIG.R.autoplay);
53-
}
54-
55-
new_link.href = new_link.href.replace(play, stop);
56-
if (new_link.rel.indexOf(CONFIG.R.autoplay) > -1 ) {
57-
new_link.rel.splice(new_link.rel.indexOf(CONFIG.R.autoplay), 1);
58-
}
59-
60-
} else if (link.rel.indexOf(CONFIG.R.autoplay) == -1) {
61-
62-
// original link isnt autoplay, leave it alone
63-
// autoplay=false may autoplay. Many publishers react to any `autoplay` value
64-
65-
if (link.href.indexOf(stop) > -1) {
66-
new_link.href = new_link.href.replace(stop, play);
67-
} else {
68-
new_link.href += (new_link.href.indexOf('?') > -1 ? '&' : '?') + play;
69-
}
70-
71-
new_link.rel.push(CONFIG.R.autoplay);
7228

29+
const stop_querystring = play_querystring.replace(/\=(\w+)/, function (p1, p2) {
30+
const antonyms = {
31+
'1': '0',
32+
'0': '1',
33+
'true': 'false',
34+
'false': 'true',
35+
'on': 'off',
36+
'off': 'on'
37+
};
38+
return '=' + antonyms[p2];
39+
});
40+
41+
// Fix original link first, if needed
42+
if (link.href.indexOf(play_querystring) > -1 && link.rel.indexOf(CONFIG.R.autoplay) === -1) {
43+
link.rel.push(CONFIG.R.autoplay);
44+
}
45+
46+
if (link.href.indexOf(stop_querystring) > -1 && link.rel.indexOf(CONFIG.R.autoplay) > -1) {
47+
link.rel.splice(link.rel.indexOf(CONFIG.R.autoplay), 1);
48+
}
49+
50+
var other = Object.assign({}, link);
51+
other.rel = [...link.rel];
52+
53+
if (link.rel.indexOf(CONFIG.R.autoplay) === -1) {
54+
55+
// Add a link with autoplay
56+
if (link.href.indexOf(stop_querystring) > -1) {
57+
other.href = other.href.replace(stop_querystring, play_querystring);
58+
} else {
59+
other.href += (other.href.indexOf('?') > -1 ? '&' : '?') + play_querystring;
7360
}
61+
other.rel.push(CONFIG.R.autoplay);
7462

75-
return {
76-
addLink: new_link
77-
};
63+
} else {
64+
65+
// Add a link without autoplay
66+
if (link.href.indexOf(play_querystring) > -1) {
67+
other.href = other.href.replace(play_querystring, stop_querystring);
68+
} else {
69+
other.href += (other.href.indexOf('?') > -1 ? '&' : '?') + stop_querystring;
70+
}
71+
other.rel.splice(other.rel.indexOf(CONFIG.R.autoplay), 1);
7872
}
79-
73+
74+
return {
75+
addLink: other
76+
};
77+
8078
} else if (link.autoplay) {
8179
delete link.autoplay; // if not a string - just ignore it
8280
}

0 commit comments

Comments
 (0)