What is the equilvent of GM_addStyle in GM4?

150 views
Skip to first unread message

Xing

unread,
Nov 12, 2017, 8:06:48 PM11/12/17
to greasemonkey-users
Dear all,

I noticed that GM_addStyle is removed in GM4. However it is an crucial function in my script and I want to keep it compatible with Tampermonkey as well. Is there a new method to do similar things in GM4?

Thanks and best wishes,
Xing

fohstick a

unread,
Nov 14, 2017, 1:13:04 PM11/14/17
to greasemonkey-users
I have the same problem. GM_addStyle() is crucial. Use these workarounds which found from stackoverflow:

function GM_addStyle_from_file(fileName) {
 
var head = document.head
   
, link = document.createElement("link")
  link
.type = "text/css"
  link
.rel = "stylesheet"
  link
.href = fileName
  head
.appendChild(link)
}

function GM_addStyle_from_string(str) {
   
var node = document.createElement('style');
    node
.innerHTML = str;
    document
.body.appendChild(node);
}

Example usage:
GM_addStyle_from_file
("https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery.ui.base.css");

GM_addStyle_from_string
(
   
".embeddedImgContainer {" +
       
"background: "+imgContainerBackgroundColor+";" +
   
"}" +
   
".embeddedImgAnchor {"+
       
"display: inline-block;"+
   
"}"
)




jerone

unread,
Nov 14, 2017, 2:50:27 PM11/14/17
to greasemonkey-users
Reply all
Reply to author
Forward
0 new messages