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

Add a GM.addStyle API, for styling CSP-restrictive pages #2724

Closed
eradrix opened this issue Nov 22, 2017 · 14 comments
Closed

Add a GM.addStyle API, for styling CSP-restrictive pages #2724

eradrix opened this issue Nov 22, 2017 · 14 comments
Labels
bankruptcy Issues that have been closed for "bug bankruptcy". Usually very old non-active issues.

Comments

@eradrix
Copy link

eradrix commented Nov 22, 2017

the current lack of scripting style is actually a complete disaster :-(

@CoolCmd
Copy link

CoolCmd commented Nov 22, 2017

addStyle is just 12 trivial lines of code, which you can easily copypaste into your script. Or even 11, because this line is not needed:
style.setAttribute('type', 'text/css');

@eradrix
Copy link
Author

eradrix commented Nov 22, 2017

Thanks !

However that makes the current lack even more incomprehensible...

@NoneGiven
Copy link
Contributor

NoneGiven commented Nov 22, 2017

@arantius I've got an issue with trying to load user styles on pages which have CSP and don't allow "self" as an origin -- so I can't just add a <style> element. GM could solve this by exposing an API that calls tabs.insertCSS() (ref). Thoughts?

@Sxderp
Copy link
Contributor

Sxderp commented Nov 22, 2017

I've got an issue with trying to load user styles on pages which have CSP and don't allow "self" as an origin -- so I can't just add a <style> element.

I think this will be hit in FF 58.

@NoneGiven
Copy link
Contributor

I filed a bug against Stylish since that's where I ran into the issue. Still, thought it might be worth considering here since GM's current APIs mostly expose privileged WebExt APIs that circumvent some content script restriction or other, and insertCSS is another one of those.

@NoneGiven
Copy link
Contributor

NoneGiven commented Nov 22, 2017

Found the bug @Sxderp referred to (also this one). When those land, my suggestions here and on Stylish will be unnecessary.

@arantius
Copy link
Collaborator

I don't know exactly what "This should work if you bundle the CSS in your extension..." means, in the linked bug. Dynamic node insertion might not work.

I'll reopen to consider CSP-safe functionality. (But at a really low priority...)

@arantius arantius reopened this Nov 23, 2017
@arantius arantius added this to the Tracking Upstream milestone Nov 23, 2017
@the8472
Copy link
Contributor

the8472 commented Nov 23, 2017

In principle this could also be implemented without waiting for FF by passing up a message to the background script and running tabs.insertCSS. But jumping through those hoops won't be necessary once that bug is implemented.

@NoneGiven
Copy link
Contributor

NoneGiven commented Nov 24, 2017

@arantius 1415352 says "This should apply to both HTML style attributes and the contents of HTML <style> nodes."

@arantius arantius changed the title is a new implement of GM.addStyle already planned ? Add a GM.addStyle API, for styling CSP-restrictive pages Nov 28, 2017
@Arkanosis
Copy link

Arkanosis commented Dec 8, 2017

In case there's no plan to add a GM.addStyle (which is very fine for me if the CSP issue is addressed otherwise), I suggest amending this blog post to make it clear that not only “As of today, there is no support for […] GM_addStyle”, but that none is planned in the future too (and maybe point to this alternative as well). That might seems obvious, but it wasn't for me (had I not found this issue, I might be waiting for the feature to be re-implemented instead of fixing my scripts right now).

Thanks!

@trlkly
Copy link

trlkly commented Dec 26, 2017

Regarding the polyfill: ever since I heard that GM_addStyle was on the chopping block, I've actually only used 3 commands to replace it. Rather than testing for HEAD, I just add the STYLE directly to the documentElement, which is always present, even at document-start.

var style = document.createElement('style'); var style.textContent = `
<Stylesheet here>
`; document.documentElement.appendChild(style);

Here's the function version that I use more rarely, which clocks in at 5 lines:

addStyle = function (css) {
  var style = document.createElement('style');
  style.textContent = css;
  document.documentElement.appendChild(style);
  return style; //optional, but convenient for changing the styling later.
};

I've tested this in GM 3 and 4 on Firefox, and TamperMonkey on Chrome, without issue, despite not being standard. And I prefer nonstandard to having it fail if the HEAD has not yet loaded (which happens surprisingly often. I've had complaints.)

I also note that UserStyles,org uses my method as a fallback in its automatically generated userscripts.

@CoolCmd
Copy link

CoolCmd commented Dec 27, 2017

Rather than testing for HEAD, I just add the STYLE directly to the documentElement, which is always present, even at document-start.

In FF57 and GM4, documentElement is not always available at document-start, at least on my machine.

@trlkly
Copy link

trlkly commented Dec 28, 2017 via email

@CoolCmd
Copy link

CoolCmd commented Dec 28, 2017

I don't even think you can set up mutation observers.

You can attach mutation observer to document.

@arantius arantius added the bankruptcy Issues that have been closed for "bug bankruptcy". Usually very old non-active issues. label Nov 2, 2023
@arantius arantius closed this as completed Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bankruptcy Issues that have been closed for "bug bankruptcy". Usually very old non-active issues.
Projects
None yet
Development

No branches or pull requests

8 participants