diff --git a/README.md b/README.md index 7191116..a26c367 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,8 @@ document.body.appendChild(el); eruda.init({ container: el, - tool: ['console', 'elements'] + tool: ['console', 'elements'], + useShadowDom: true // Use shadow dom to prevent page css affecting eruda. }); ``` diff --git a/doc/README_CN.md b/doc/README_CN.md index a6893f1..2d09798 100644 --- a/doc/README_CN.md +++ b/doc/README_CN.md @@ -85,7 +85,8 @@ document.body.appendChild(el); eruda.init({ container: el, - tool: ['console', 'elements'] + tool: ['console', 'elements'], + useShadowDom: true }); ``` diff --git a/src/index.js b/src/index.js index 166ed3f..34db1d9 100644 --- a/src/index.js +++ b/src/index.js @@ -29,12 +29,12 @@ import { } from './lib/util'; module.exports = { - init({container, tool, autoScale = true} = {}) + init({container, tool, autoScale = true, useShadowDom = true} = {}) { this._isInit = true; this._scale = 1; - this._initContainer(container); + this._initContainer(container, useShadowDom); this._initStyle(); this._initDevTools(); this._initEntryBtn(); @@ -137,7 +137,7 @@ module.exports = { if (!this._isInit) logger.error('Please call "eruda.init()" first'); return this._isInit; }, - _initContainer(el) + _initContainer(el, useShadowDom) { if (!el) { @@ -145,6 +145,28 @@ module.exports = { document.documentElement.appendChild(el); } + let shadowRoot; + if (useShadowDom) + { + if (el.attachShadow) + { + shadowRoot = el.attachShadow({mode: 'open'}); + } else if (el.createShadowRoot) + { + shadowRoot = el.createShadowRoot(); + } + if (shadowRoot) + { + // font-face doesn't work inside shadow dom. + evalCss.container = document.head; + evalCss(require('./style/icon.css')); + + el = document.createElement('div'); + shadowRoot.appendChild(el); + this._shadowRoot = shadowRoot; + } + } + Object.assign(el, { id: 'eruda', className: 'eruda-container', @@ -165,9 +187,16 @@ module.exports = { let className = 'eruda-style-container', $el = this._$el; - $el.append(`
`); + if (this._shadowRoot) + { + evalCss.container = this._shadowRoot; + evalCss(':host { all: initial }'); + } else + { + $el.append(`
`); + evalCss.container = $el.find(`.${className}`).get(0); + } - evalCss.container = $el.find(`.${className}`).get(0); evalCss( require('./style/style.scss') + require('./style/reset.scss') + diff --git a/test/boot.js b/test/boot.js index 189bcce..e62a767 100644 --- a/test/boot.js +++ b/test/boot.js @@ -3,7 +3,9 @@ function boot(name, cb) // Need a little delay to make sure width and height of webpack dev server iframe are initialized. setTimeout(function () { - var options = {}; + var options = { + useShadowDom: false + }; if (name) { options.tool = name === 'settings' ? [] : name; diff --git a/test/eruda.js b/test/eruda.js index 594d852..d465083 100644 --- a/test/eruda.js +++ b/test/eruda.js @@ -17,7 +17,8 @@ describe('devTools', function () eruda.init({ container: container, - tool: [] + tool: [], + useShadowDom: false }); var $eruda = $('#eruda'); diff --git a/test/index.html b/test/index.html index dfffe05..20a4a63 100644 --- a/test/index.html +++ b/test/index.html @@ -50,7 +50,10 @@ setTimeout(function () { try { - eruda.init({container: el}); + eruda.init({ + container: el, + useShadowDom: false + }); } catch (e) { alert(e); diff --git a/test/init.js b/test/init.js index da9db87..7fd74b6 100644 --- a/test/init.js +++ b/test/init.js @@ -1 +1,3 @@ -eruda.init(); +eruda.init({ + useShadowDom: false +}); diff --git a/test/manual.html b/test/manual.html index 21cc0b6..13ae25b 100644 --- a/test/manual.html +++ b/test/manual.html @@ -6,7 +6,6 @@ Manual - @@ -34,6 +33,9 @@
  • Big Array
  • +
  • + Override Style +
  • + -