You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to use Shadow DOM in order to isolate my userscript's popup from the page's CSS, and the page from my popup's CSS.
Also it will hide the popup's HTML from body.textContent, for example.
It's easy to do for usual sites.
However, on sites with enabled CSP I need to use GM_addStyle to add styles. But it (TM/VM) adds the styles into the head element.
So, it's impossible to style Shadow DOM on these sites (for example, https://mastodon.social/home).
I want to be able to specify the place where to inject the style element.
Like this:
GM_addStyle(cssText,shadowWrapper.shadowRoot);
Script
// ==UserScript==// @name Shadow DOM Style// @namespace gh.alttiri// @version 0.2// @description Adds Shadow DOM// @match https://mastodon.social/*// @grant GM_addStyle// ==/UserScript==letshadowWrapper=document.createElement("div");document.querySelector("body").append(shadowWrapper);// "html"shadowWrapper.setAttribute("id","ujs-outer-shadow-wrapper");shadowWrapper.attachShadow({mode: "open"});shadowWrapper.shadowRoot.innerHTML=`<div id="shadow-content-wrapper"> <h1>Shadow header</h1> <div>Shadow content</div> <div> <span>1</span> <span>2</span> <span>3</span> </div> <!-- Inline styles will not work on CSP sites. --> <style>h1 + div:after { content: " (on non CSP-site)"; color: green; }</style></div>`;shadowWrapper.shadowRoot.querySelector("h1").onclick=()=>console.log("clicked");letcssText=`#shadow-content-wrapper { position: fixed; padding: 24px; margin: 12px; font-weight: bold; background-color: white; border-radius: 8px; }h1 { color: darkorange; }div { color: red; }`;functionaddCSS(css,target=document.head){conststyleElem=document.createElement("style");styleElem.textContent=css;target.append(styleElem);returnstyleElem;}// 1.addCSS(cssText,shadowWrapper.shadowRoot);// Works fine, but only on sites without CSP// 2.// It works on sites with CSP, but I need to inject CSS into the shadow DOM element, not into "head" tag.// GM_addStyle(cssText);// 3.// I want something like this// GM_addStyle(cssText, shadowWrapper.shadowRoot);
(Note: I updated the example code to make it more common.)
The text was updated successfully, but these errors were encountered:
There is another way, which is even more preferable when you add multiple shadows due to re-using already parsed styles
constcss=`#href-taker-shadow {color: red;}`;constsheet=newCSSStyleSheet();// you may use replace() method as well, but I had a flickering issue in FF with itsheet.replaceSync(css);wrapper.shadowRoot.adoptedStyleSheets=[sheet];
I want to use Shadow DOM in order to isolate my userscript's popup from the page's CSS, and the page from my popup's CSS.
Also it will hide the popup's HTML from
body.textContent
, for example.It's easy to do for usual sites.
However, on sites with enabled CSP I need to use
GM_addStyle
to add styles. But it (TM/VM) adds the styles into thehead
element.So, it's impossible to style Shadow DOM on these sites (for example, https://mastodon.social/home).
I want to be able to specify the place where to inject the style element.
Like this:
Script
(Note: I updated the example code to make it more common.)
The text was updated successfully, but these errors were encountered: