Giter VIP home page Giter VIP logo

shadow-dom-inject-styles's Introduction

Shadow-DOM-inject-styles

A helper function to easily modify Shadow DOM CSS.

Install

npm install shadow-dom-inject-styles --save

Vanilla JS Example / Demo

jsfiddle

<script type="module">
  import {injectStyles} from 'https://unpkg.com/shadow-dom-inject-styles@latest/dist/index.js';

  setTimeout(() => {

    const toolbar = document.querySelector('ion-app > ion-header > ion-toolbar');

    // language=CSS
    const styles = `
        .toolbar-background {
            background: red !important;
        }
        .toolbar-container {
            color: #fff !important;
        }
    `;

    injectStyles(toolbar, '.toolbar-background', styles);

  }, 200);
</script>

Typescript Example

import {injectStyles} from 'shadow-dom-inject-styles';

const toolbar = (this.el.querySelector('ion-header > ion-toolbar') as HTMLElement);

// language=CSS
const styles = `
  .toolbar-background {
    background: red !important;;
  }
`;

injectStyles(toolbar, '.toolbar-background', styles);

shadow-dom-inject-styles's People

Contributors

adamlacombe avatar theonlymrflow avatar tonn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

shadow-dom-inject-styles's Issues

Style not injected when no existing style element present in shadow DOM

  const root = shadowRootElement.shadowRoot;
  let styleAlreadyAdded = false;
  const currentStyleTags = Array.from(root.querySelectorAll('style'));
  currentStyleTags.forEach((element: HTMLStyleElement, index) => {
    if (element.innerHTML === styles) {
      styleAlreadyAdded = true;
    }
    if (currentStyleTags.length - 1 === index && styleAlreadyAdded === false) {
      const newStyleTag = document.createElement('style');
      newStyleTag.innerHTML = styles;
      root.insertBefore(newStyleTag, root.querySelector(insertBeforeSelector));
    }
  });

If there are no style elements already within the shadow DOM, currentStyleTags will be an empty array and the code path that conditionally injects the new styles never executes.

big bug

there is a bug: if there isn't any <style> tag in the shadowRootElement, this function can't work, because it use forEach of style tag elements.

Typescript example for multi DOM object

Maybe someone need this:

[].forEach.call(document.querySelectorAll('ion-button'), function(div) {
      const styles = `
      button {
        background: red !important;
      }
      `;
      injectStyles(div, 'button', styles);
    });

selenium java shadow DOM

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting ChromeDriver 90.0.4430.24 (4c6d850f087da467d926e8eddb76550aed655991-refs/branch-heads/4430@{#429}) on port 16826
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
May 27, 2021 2:39:18 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
May 27, 2021 2:39:18 PM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
WARNING: Unable to find an exact match for CDP version 90, so returning the closest version found: 86
May 27, 2021 2:39:18 PM org.openqa.selenium.devtools.CdpVersionFinder findNearestMatch
INFO: Found CDP implementation for version 90 of 86
Exception in thread "main" org.openqa.selenium.JavascriptException: javascript error: Cannot read property 'shadowRoot' of null
(Session info: chrome=90.0.4430.212)
Build info: version: '4.0.0-alpha-7', revision: 'de8579b6d5'
System info: host: 'LAPTOP-OT58K9N8', ip: '192.168.1.101', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '15.0.2'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 90.0.4430.212, chrome: {chromedriverVersion: 90.0.4430.24 (4c6d850f087da..., userDataDir: C:\Users\SRIKAN~1\AppData\L...}, goog:chromeOptions: {debuggerAddress: localhost:58595}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: c9d0c3a102710fe4b62167e0861c02be
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:196)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:129)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:167)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:123)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:501)
at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:436)
at order.Shadowdom.main(Shadowdom.java:22)

TypeError: Cannot read property 'shadowRoot' of null

I used shadow-DOM-inject-styles as follow:
constructor(private el: ElementRef) {
const tabbar = this.el.nativeElement.querySelector('ion-tabs > ion-tabbar') as HTMLElement;
const styles = .tabbar-background { background: red !important;; };
injectStyles(tabbar, '.tabbar-background', styles);
}
but i got an error as below:
TypeError: Cannot read property 'shadowRoot' of null

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.