使用Quasar bex将Vue组件注入网页

dkqlctbz  于 2022-12-19  发布在  Vue.js
关注(0)|答案(1)|浏览(171)

我正在使用Quasar BEX构建一个浏览器扩展,我想在加载的网页中显示一个Vue组件。我已经尝试使用内容脚本钩子在网页中添加原始html和css,但我想从我的src/components文件夹中添加一个组件,该文件夹也使用Quasar组件,如q-btn等。
有没有办法做到这一点?

q35jwt9p

q35jwt9p1#

好的,我已经解决了这个问题,我正在编写解决方案,所以其他人可能会发现它很有用。我使用iframe将组件或页面注入到加载的网页中
首先我创建了一个新的路由,比如test inroutes.jslike

{
  name: "test",
  path: "/test",
  component: () => import("pages/test.vue"),
},

然后,我创建了一个iframe,并在content-script.js中加载了该特定路由,例如

function createIframe() {
  const iframe = document.createElement("iframe");
  iframe.width = "220px";
  iframe.height = "220px";

  Object.assign(iframe.style, {
    position: "fixed",
    border: "none",
    zIndex: "10000",
  });

  // load that specific page 
  iframe.src = chrome.runtime.getURL("www/index.html#test");

  return iframe;
}

你可以设置你的iframe的宽度和高度以及其他你可能需要的东西,然后你可以把它作为一个元素注入到任何地方,比如

document.body.prepend(createIframe());

我们开始吧!:)

相关问题