question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

insertCss not working

See original GitHub issue

Is it possible to call the insertCSS function of electron webView();? if so how do I do that? I tried

tab = tabGroup.addTab({
        title: 'Electron',
        src: path.join(appBase, '/views/main-frame/index.html')
        visible: true,
        active: true,
        webviewAttributes: {
          nodeintegration: true,
          plugins: true
        },
        ready: function (tab) {
          tab.webview.insertCSS("background-color:red;");
        }
    });

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
brrdcommented, Feb 13, 2017

Sorry, this is my mistake! Please forget my previous post. Actually you were totally right with your first code, but as it’s specified in electron documentation (here):

The webview element must be loaded before using the methods.

So you must wait for the webview to be loaded before using insertCSS.

Another mistake is the parameter you use in insertCSS. It is supposed to be a valid CSS rule with a selector.

So this code should work finaly:

tab = tabGroup.addTab({
        title: 'Electron',
        src: path.join(appBase, '/views/main-frame/index.html')
        visible: true,
        active: true,
        webviewAttributes: {
          nodeintegration: true,
          plugins: true
        },
        ready: function (tab) {
	  var webview = tab.webview;
	  webview.addEventListener('did-finish-load', function () {
	    webview.insertCSS("body{ background-color: red;}");
	  });
        }
    });

Hope this helps.

1reaction
brrdcommented, Feb 13, 2017

By the way, I just wrote feature request #9 which can help to handle this in a more convenient way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chrome Extension to change Style Elements, insertCSS not ...
I have a working extension with popup HTML and js files. I tried using insertCSS in the content.js but read that it will...
Read more >
Chrome Extension to change Style Elements, insertCSS not ...
I have a working extension with popup HTML and js files. I tried using insertCSS in the content.js but read that it will...
Read more >
tabs.insertCSS() - Mozilla - MDN Web Docs
To work cross-browser, you can specify the path as an absolute URL, starting at the extension's root, like this: "/path/to/stylesheet.css" .
Read more >
WebContents#insertCSS does not work when 'sandbox: true'
Download and unarchive the Gist · Enter the unarchived directory and run npm install · Run ./node_modules/.bin/electron . · Text in <body> is...
Read more >
How to add CSS - W3Schools
Three Ways to Insert CSS. There are three ways of inserting ... The external .css file should not contain any HTML tags. Here...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found