Skip to content
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

GM_addStyle does not work with the shadow DOM #1059

Closed
barrer opened this issue Nov 1, 2020 · 4 comments
Closed

GM_addStyle does not work with the shadow DOM #1059

barrer opened this issue Nov 1, 2020 · 4 comments
Milestone

Comments

@barrer
Copy link

barrer commented Nov 1, 2020

Expected Behavior

work with the shadow DOM
https://developer.mozilla.org/en-US/docs/Web/API/Element/attachShadow

Actual Behavior

not work with the shadow DOM

Specifications

I wrote a script that uses element.attachShadow(), but encountered a restriction in Content-Security-Policy, which prevented the CSS style file from loading, and GM_addStyle doesn't work under shadow DOM. Because the style is complex, I don't want to interfere with the page, so I use shadow DOM.

Script address:

https://greasyfork.org/en/scripts/376313-%E5%88%92%E8%AF%8D%E7%BF%BB%E8%AF%91-%E5%A4%9A%E8%AF%8D%E5%85%B8%E6%9F%A5%E8%AF%A2

https://greasyfork.org/en/scripts/34921-translate

Website restricted by CSP: https://www.gimp.org/

  • Google Chrome: Version 86.0.4240.111 (Official Build) (64-bit)
  • TM: 4.11
  • OS: Windows 10
@barrer barrer changed the title How to use GM_addStyle in Tampermonkey on #shadow-root? GM_addStyle does not work with the #shadow-root element Nov 2, 2020
@barrer barrer changed the title GM_addStyle does not work with the #shadow-root element GM_addStyle does not work with the shadow DOM Nov 2, 2020
@tophf
Copy link

tophf commented Nov 6, 2020

You can override attachShadow prototype in the page by using @grant none or unsafeWindow or unsafeWindow.wrappedJSObject (in Firefox). In your hook function you can add a style element into the shadow root or extend its adoptedStyleSheets setter to always add that style sheet object. Here's an example for an extension which can be adapted to a userscript.

@derjanb derjanb added this to the 4.12 milestone Nov 6, 2020
@barrer
Copy link
Author

barrer commented Nov 9, 2020

@tophf
The experimental API DocumentOrShadowRoot.adoptedStyleSheets works great in Chrome! Thank you very much!

function adoptedStyleSheets(bindDocumentOrShadowRoot, cssText) {
    try {
        if (bindDocumentOrShadowRoot.adoptedStyleSheets) {
            cssText = cssText.replace(/\/\*.*?\*\//ig, ''); // remove CSS comments
            var cssSheet = new CSSStyleSheet();
            var styleArray = cssText.split('\n');
            for (var i = 0; i < styleArray.length; i++) {
                var line = styleArray[i].trim();
                if (line.length > 0) {
                    cssSheet.insertRule(line);
                }
            }
            bindDocumentOrShadowRoot.adoptedStyleSheets = [cssSheet];
        }
    } catch (error) {
        log(error);
    }
}

@barrer barrer closed this as completed Nov 9, 2020
@ssbarnea
Copy link

ssbarnea commented Feb 2, 2021

Can someone guide me some hints on how to adapt a very simple extension that just used GM_addStyle() to bypass this? I suspect that reason why it fails to inject the styles is due to shadow use.

@derjanb
Copy link
Member

derjanb commented Feb 2, 2021

@ssbarnea Please see #881 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants