问题描述
我试图在Chrome扩展程序中创建自定义元素,但customeElements.define
是null
.
I attempted to create a custom element in a Chrome extension but customeElements.define
is null
.
customElements.define('customElement', class extends HTMLElement {
constructor() {
super();
}
...
});
因此,显然Chrome浏览器不希望扩展程序创建自定义元素.但为什么?有安全隐患吗?
So apparently Chrome doesn't want extensions to create custom elements. But why? Is it a security risk?
我似乎无法在Chrome扩展指南中找到任何不允许的内容.
I can't seem to find anything in Chrome's extension guide that says it's not allowed.
推荐答案
您可能需要先检查此文章,以充分了解自定义元素的作用以及如何构建它们.如前所述,在构建自定义元素时,您还需要检查document.registerElement
是否可用.否则,您只需加载webcomponents.js
即可对其进行填充.
You may want to first check this article to fully understand what custom elements do and how to build them. As discussed, in building a custom element, you also need to check if document.registerElement
is available. Otherwise, you can simply load webcomponents.js
to polyfill it.
这里是您可能需要检查其他洞察力的其他参考.
Here are additional references that you may want to check for additional insights.
- 自定义元素v1:可重复使用的Web组件.此处列举了有关创建自定义元素的规则.
- 注册chrome扩展名中的自定义元素.这需要使用
webcomponentsjs polyfill
.Chrome会阻止在Chrome扩展程序中执行本机registerElement
.
- Custom Elements v1: Reusable Web Components. Rules on creating custom elements are enumerated here.
- Registering a custom element from a chrome extension. This requires the usage of the
webcomponentsjs polyfill
.Chrome will prevent nativeregisterElement
from being executed within a chrome extension.
这篇关于为什么不能在Chrome扩展程序中创建自定义元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!