简体   繁体   English

Chrome 扩展 VueJS:如何在网页上创建叠加层?

[英]Chrome Extension VueJS : how to create overlay over web page?

I'm creating a Chrome Extension App with VueJS.我正在使用 VueJS 创建一个 Chrome 扩展应用程序。 With google chrome extensions you can push some HTML in other web page, I want to do the same thing with my VueJS App.使用 google chrome 扩展,你可以在其他网页中推送一些 HTML,我想用我的 VueJS 应用程序做同样的事情。 I've created a file that is supposed to push my app in the body of any page but it doesn't work.我创建了一个文件,该文件应该将我的应用程序推送到任何页面的正文中,但它不起作用。

There is the file :有文件:

import App from './App'

const element = document.querySelector('body').firstChild
const anchor = document.createElement('DIV')
anchor.insertBefore(element)

new Vue({
    el: anchor,
    render: h => h(App)
})

I've also my popup.js who as the same utilities than main.js.我还有我的 popup.js,它与 main.js 具有相同的实用程序。 This file is for the creation of my app, there it is :该文件用于创建我的应用程序,它是:

import Vue from 'vue'
import App from './App'
import router from './router/index'
import { fb } from './firebase/init'

let app = null;


fb.auth().onAuthStateChanged(() => {
  if(!app){
    app = new Vue({
      el: '#app',
      router,

      render: h => h(App)
    })
  }
}) 

And there is my content-script in the manifest.json : manifest.json 中有我的内容脚本:

{
  "name": "******",
  "description": "VueJS Extension",
  "version": null,
  "manifest_version": 2,
  "icons": {
    "128": "icons/******.png"
  },
  "browser_action": {
    "default_title": "*****",
    "default_popup": "popup/popup.html"
  },
  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
  "background": {
    "scripts": [
      "background.js"
    ]
  },
  "permissions": [
    "identity"
  ],
  "oauth2": {
    "client_id": "**************",
    "scopes": [
      "https://apis.google.com"
    ]
  },
  "content_scripts": [
    {
      "matches": ["http://*/*"],
      "js": ["print.js"]
    }
  ]
}

If someone have any clue, it would be cool ;)如果有人有任何线索,那就太酷了;)

One issue is this一个问题是这个

const element = document.querySelector('body').firstChild
const anchor = document.createElement('DIV')
anchor.insertBefore(element)

now, according to documentation ..., insertBefore works like this现在,根据文档...,insertBefore 像这样工作

parentNode.insertBefore(newNode, referenceNode);

so, your parentNode is a newly created node that isn't in the DOM所以,你的parentNode是一个新创建的节点,它不在 DOM 中

your newNode is the body's first child你的newNode是身体的第一个孩子

and reference node is null (so, you're inserting the node at the end)并且参考节点为空(因此,您在最后插入节点)

you're effectively doing this你正在有效地这样做

const element = document.body.firstChild
const anchor = document.createElement('DIV')
anchor.appendChild(element)

what you probably wanted to do was你可能想做的是

const element = document.body.firstChild
const anchor = document.createElement('DIV')
document.body.insertBefore(anchor, element)

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

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