简体   繁体   English

如何使用 chrome 扩展程序在网页中注入模板组件?

[英]How to inject a stencil's component inside a webpage using a chrome extension?

I have been exploring the possibility of injecting one or more stencil's web-component to any web-page on inte.net by using a chrome extension.我一直在探索通过使用 chrome 扩展将一个或多个模板的网络组件注入到 inte.net 上的任何网页的可能性。 However the lazy loading feature of stenciljs has turned out to be an obstacle in my case.然而,事实证明,stenciljs 的延迟加载功能对我来说是一个障碍。

In the first step,I loaded the core files ie component.core.js and component.js and entry file of my component, by listing them in manifest.json (in web_accessible_resources array)在第一步中,我加载了核心文件,即 component.core.js 和 component.js 以及我的组件的入口文件,将它们列在 manifest.json(在 web_accessible_resources 数组中)

manifest.json清单.json

"web_accessible_resources": [
"js/web-component.js",
"js/web-components/web-components.core.js",
"js/web-components/my-component.entry.js",]

Then appended them to the webpage as a script element.然后将它们作为脚本元素附加到网页。 This worked fine.这很好用。

Problem arises as soon as I add one of my component to the DOM, Stencil tries to lazy load the component.entry.js file from the resources folder whose path I must provide either from stencil.config.ts or as data-resources-url attribute on the script tag of component.core.js in the DOM.一旦我将我的一个组件添加到 DOM,就会出现问题,Stencil 会尝试从资源文件夹中延迟加载 component.entry.js 文件,我必须从 stencil.config.ts 或 data-resources-url 提供其路径DOM 中 component.core.js 的脚本标签上的属性。

Now I am not sure how can I provide stencil the correct path to component's resources that are located in extension?现在我不确定如何为模板提供位于扩展中的组件资源的正确路径?

I was able to successfully inject my stenciljs component with shadowDOM enabled using chrome extension.我能够成功地注入我的 stenciljs 组件,并使用 chrome 扩展启用了 shadowDOM。 To be exact I was looking for a way to get the path of files inside extension in your js and I found it.确切地说,我正在寻找一种方法来获取 js 中扩展名内的文件路径,我找到了它。

chrome.runtime.getURL('your relative url')

Steps to inject stencil component using extension are as follows使用扩展注入stencil组件的步骤如下

1- Deploy your core files to extension's build folder 1- 将核心文件部署到扩展的构建文件夹

Deploy your component.core.js , component.js and my-component.entry.js files to extension's build folder将您的 component.core.js 、 component.js 和 my-component.entry.js 文件部署到扩展的构建文件夹

2- Add files to web_accessible_resources 2- 将文件添加到 web_accessible_resources

whitelist the files you just added in manifest.json将您刚刚在 manifest.json 中添加的文件列入白名单

"web_accessible_resources": [
 "js/web-component.js",
 "js/web-components/web-components.core.js",
 "js/web-components/my-component.entry.js",]

3- append js files to DOM and set data-resources-url 3- 将 js 文件附加到 DOM 并设置 data-resources-url

only add component.js file in the DOM by creating a script element Rest of the files will be lazy loaded by stencil.仅通过创建脚本元素在 DOM 中添加 component.js 文件,其余文件将由 stencil 延迟加载。

Set attribute data-resources-url equal to the folder where all three files are present.设置属性 data-resources-url 等于所有三个文件所在的文件夹。 Don't forget to provide absolute path using chrome.runtime.getURL('your relative url') on both src and data-resources-url attribute.不要忘记在 src 和 data-resources-url 属性上使用chrome.runtime.getURL('your relative url')提供绝对路径。 Also make sure your script is appended after the last script that is present on the page else it wont work (this is bug in stencil's code).还要确保您的脚本附加在页面上存在的最后一个脚本之后,否则它将无法工作(这是模板代码中的错误)。

your script will look like this:您的脚本将如下所示:

<script src="chrome-extension://dlahaeelpopfeas/js/component.js" data-resources-url="chrome-extension://dlahaeelpopfeas/js/"></script>

Now you can append your component in DOM and use it.现在您可以在 DOM 中附加您的组件并使用它。

I managed to make it work, but for me it was a little different.我设法让它发挥作用,但对我来说有点不同。

manifest.json清单.json

"web_accessible_resources": [
   "build/*"
]

index.js索引.js

const script = document.createElement('script');

script.setAttribute('type', 'module');
script.setAttribute('src', chrome.runtime.getURL('build/my-component.esm.js'));
script.setAttribute('data-resources-url', chrome.runtime.getURL('build/'));
script.setAttribute('data-stencil-namespace', 'my-component');

document.body.appendChild(script);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM