使用Chrome扩展程序覆盖Element.prototype.attachShadow

时间:2019-03-02 01:24:59

标签: javascript google-chrome-extension shadow-dom

对于某些Selenium测试,我需要访问具有封闭Shadow DOM的Web组件的DOM。我已经阅读了一些参考资料,您可以在文档启动时覆盖Element.prototype.attachShadow,以将“阴影”从关闭更改为打开。为此,我创建了一个Chrome扩展程序。以下是我的manifest.json

{
    "name": "SeleniumTesting",
    "description": "Extension to open closed Shadow DOM for selenium testing",
    "version": "1",
    "author": "SeleniumTesting",
    "manifest_version": 2,
    "permissions": ["downloads", "<all_urls>"],
    "content_scripts": [{
        "matches": ["http://localhost:5000/*"],
        "run_at": "document_start",
        "all_frames": true,
        "js": ["shadowInject.js"]
    }]
}

还有我的 shadowInject.js

console.log('test');
Element.prototype._attachShadow = Element.prototype.attachShadow;
Element.prototype.attachShadow = function () {
    console.log('attachShadow');
    return this._attachShadow( { mode: "open" } );
};

为了对其进行测试,我在ASPNetCore MVC项目中创建了我的组件。以下是我的JavaScript,用于创建自定义组件:

customElements.define('x-element', class extends HTMLElement {
    constructor() {
        super(); 
        this._shadowRoot = this.attachShadow({
            mode: 'closed'
        });
        this._shadowRoot.innerHTML = `<div class="wrapper">
        <a href="download/File.pdf" download>
            <button id="download">Download File</button>
        </a>
        <p>Link para download</p>
      </div>`;
    }
});

以及使用它的我的HTML文件:

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}
<script src='~/js/componente.js'></script>

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
    <x-element id='comp'></x-element>
</div>

我将扩展程序加载到Chrome中,然后运行该页面。我得到了控制台日志Test,但是从未调用attachShadow方法,而且仍然无法访问封闭的影子DOM

Console log

我真的很感谢我做错了什么。非常感谢。

最终解决方案

在对答案进行更改后,我需要对manifest.json进行一些调整。下面是最终版本:

{
    "name": "SeleniumTesting",
    "description": "Extension to open closed Shadow DOM for selenium testing",
    "version": "1",
    "author": "SeleniumTesting",
    "manifest_version": 2,
    "permissions": ["downloads", "<all_urls>"],
    "content_scripts": [{
        "matches": ["http://localhost:5000/*"],
        "run_at": "document_start",
        "all_frames": true,
        "js": ["shadowInject.js"]
    }],
    "web_accessible_resources": ["injected.js"]
}

现在可以使用了,并且Shadow DOM更改为打开 enter image description here

2 个答案:

答案 0 :(得分:3)

您不应将代码放在content_scripts中,因为content_scripts与当前页面上下文不同。

您尝试将shadowInject.js代码更改为:

const injectedScript = document.createElement('script');
injectedScript.src = chrome.extension.getURL('injected.js');
(document.head || document.documentElement).appendChild(injectedScript);

然后在同一目录中创建一个injected.js文件:

文件内容为:

console.log('test');
Element.prototype._attachShadow = Element.prototype.attachShadow;
Element.prototype.attachShadow = function () {
    console.log('attachShadow');
    return this._attachShadow( { mode: "open" } );
};

您可以尝试。如果有任何问题,请告诉我

答案 1 :(得分:1)

如Black-Hole的答案所述,内容脚本具有其自己的DOM对象版本,因此您必须注入其他脚本才能在真实DOM中运行。

如果您想尽可能少地触摸页面并使阴影与页面的其余部分保持闭合,则可以使用以下脚本:

{
  const shadows = new WeakMap();
  const original = Element.prototype.attachShadow;
  Element.prototype.attachShadow = function() {
    const shadow = original.apply(this, arguments);
    shadows.set(this, shadow);
    return shadow;
  };

  // Later...
  shadows.get(document.querySelector("x-element"));
}