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.

Applying CSS to a website fails

See original GitHub issue

I am trying to apply a CSS to a website like so:

await Page.enable();
await DOM.enable();
await CSS.enable();
const frame = await Page.navigate({ url: 'https://google.com' });
const styleSheet = await CSS.createStyleSheet(frame)


const ruleText = 'body { background-color: red; }';
const location = {
  startLine: 0,
  endLine: 0, // should be split by \n if its multiline
  startColumn: 0,
  endColumn: 31, // guessing this has to be the length if it is a one liner? what about multi line? longest line?
};
await CSS.addRule(styleSheet, ruleText, location);

It fails with Error: Invalid parameters and 'ruleText: string value expected; location: object expected'

Any idea what is happening? I can’t find anything in the documentation

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
cyrus-andcommented, Jun 7, 2017

This works for me:

const CDP = require('chrome-remote-interface');

CDP(async (client) => {
    try {
        const {CSS, DOM, Page} = client;
        // the CSS domain must be enabled
        await DOM.enable();
        await CSS.enable();
        await Page.enable();
        // load the URL and wait for the load event in order to alter the CSS
        const {frameId} = await Page.navigate({url: 'https://google.com'});
        await Page.loadEventFired();
        // create and populate a new stylesheet so there's no need to mess
        // with ranges which is quite annoying; no !important is needed now as
        // presumably this new stylesheet has the highest priority
        const {styleSheetId} = await CSS.createStyleSheet({frameId});
        await CSS.setStyleSheetText({
            styleSheetId,
            text: `body { background: red; }`
        });
    } catch (err) {
        console.error(err);
    } finally {
        client.close();
    }
}).on('error', (err) => {
    console.error(err);
});
0reactions
dominikkukackacommented, Jun 8, 2017

Ah perfect.

Thank you very much for your support!!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Troubleshoot CSS Not Working - WPForms
Have you added custom CSS for your forms, but not been able to see it come through? Here's how to troubleshoot when your...
Read more >
Handling common HTML and CSS problems - MDN Web Docs
CSS is arguably better at fallbacks than HTML. If a browser encounters a declaration or rule it doesn't understand, it just skips it...
Read more >
Understanding why your CSS fails - LogRocket Blog
It can be tricky to find out why your CSS isn't working as expected. Learn ways of finding and preventing weird behavior in...
Read more >
Complete list of reasons why a css file might not be working
3. The URL you're using inside your HTML link tag may be unaccessable, so manually try to visit the stylesheet with a browser...
Read more >
How to Fix CSS file is not Working with HTML in Chrome
Fix CSS file is not Working with HTML in Chrome | Problem Solved ... In HTML CSS Website | Website Images Not Showing...
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