Skip to content

Update styletag injection to use <style> for platforms other than firefox #508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,25 @@ export function getInjectionElement () {
return document.head || document.documentElement
}

// Tests don't define this variable so fallback to behave like chrome
const hasMozProxies = typeof mozProxies !== 'undefined' ? mozProxies : false

/**
* Creates a script element with the given code to avoid Firefox CSP restrictions.
* @param {string} css
* @returns {HTMLLinkElement}
* @returns {HTMLLinkElement | HTMLStyleElement}
*/
export function createStyleElement (css) {
const style = document.createElement('link')
style.href = 'data:text/css,' + encodeURIComponent(css)
style.setAttribute('rel', 'stylesheet')
style.setAttribute('type', 'text/css')
let style
if (hasMozProxies) {
style = document.createElement('link')
style.href = 'data:text/css,' + encodeURIComponent(css)
style.setAttribute('rel', 'stylesheet')
style.setAttribute('type', 'text/css')
} else {
style = document.createElement('style')
style.innerText = css
}
return style
}

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

// Tests don't define this variable so fallback to behave like chrome
const hasMozProxies = typeof mozProxies !== 'undefined' ? mozProxies : false

// linear feedback shift register to find a random approximation
export function nextRandom (v) {
return Math.abs((v >> 1) | (((v << 62) ^ (v << 61)) & (~(~0 << 63) << 62)))
Expand Down