Skip to content

Commit ec91238

Browse files
Update styletag injection to use <style> for platforms other than firefox (#508)
* update styletag injection to use <style> for platforms other than firefox * lint * specify HTMLStyleElement Co-authored-by: Jonathan Kingston <[email protected]> * fix typo --------- Co-authored-by: Jonathan Kingston <[email protected]>
1 parent 7da740e commit ec91238

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/utils.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,25 @@ export function getInjectionElement () {
2323
return document.head || document.documentElement
2424
}
2525

26+
// Tests don't define this variable so fallback to behave like chrome
27+
const hasMozProxies = typeof mozProxies !== 'undefined' ? mozProxies : false
28+
2629
/**
2730
* Creates a script element with the given code to avoid Firefox CSP restrictions.
2831
* @param {string} css
29-
* @returns {HTMLLinkElement}
32+
* @returns {HTMLLinkElement | HTMLStyleElement}
3033
*/
3134
export function createStyleElement (css) {
32-
const style = document.createElement('link')
33-
style.href = 'data:text/css,' + encodeURIComponent(css)
34-
style.setAttribute('rel', 'stylesheet')
35-
style.setAttribute('type', 'text/css')
35+
let style
36+
if (hasMozProxies) {
37+
style = document.createElement('link')
38+
style.href = 'data:text/css,' + encodeURIComponent(css)
39+
style.setAttribute('rel', 'stylesheet')
40+
style.setAttribute('type', 'text/css')
41+
} else {
42+
style = document.createElement('style')
43+
style.innerText = css
44+
}
3645
return style
3746
}
3847

@@ -53,9 +62,6 @@ export function setGlobal (globalObjIn) {
5362
Error = globalObj.Error
5463
}
5564

56-
// Tests don't define this variable so fallback to behave like chrome
57-
const hasMozProxies = typeof mozProxies !== 'undefined' ? mozProxies : false
58-
5965
// linear feedback shift register to find a random approximation
6066
export function nextRandom (v) {
6167
return Math.abs((v >> 1) | (((v << 62) ^ (v << 61)) & (~(~0 << 63) << 62)))

0 commit comments

Comments
 (0)