[英]Inject Popper.js in Chrome Extension Content script
This is my flow: Content script -> Fetch data using background.js -> Return data and inject the new HTML into the webpage.这是我的流程:内容脚本 -> 使用 background.js 获取数据 -> 返回数据并将新的 HTML 注入网页。
Now I have to show a tooltip using Popper.js but I get the error "document" is undefined.现在我必须使用 Popper.js 显示一个工具提示,但我得到错误“文档”未定义。 It's strange because I am in content-scripts.js.
这很奇怪,因为我在 content-scripts.js 中。 How can I get the newly updated dom after my injection?
注入后如何获取新更新的 dom?
content-scripts.js内容脚本.js
chrome.runtime.sendMessage({command: "sales_data", data: payload}, function(items) {
let view = new SearchResultsView(items)
view.injectToDom()
const popcorn = document.querySelector('#popcorn'); // document is undefined
const tooltip = document.querySelector('#tooltip'); // document is undefined
createPopper(popcorn, tooltip, {
placement: 'top',
});
});
SearchResultsView/InjectDom() SearchResultsView/InjectDom()
let html = `<div id="popcorn" aria-describedby="tooltip"></div>
<div id="tooltip" role="tooltip">
My tooltip
<div id="arrow" data-popper-arrow></div>
</div>`
//...
element.insertAdjacentHTML('afterbegin', html);
SOLVED.解决了。
I had to import tippy.js.我不得不导入tippy.js。
import { createPopper } from '@popperjs/core';
import tippy from 'tippy.js';
tippy('#button', {
content: 'My tooltip!',
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.