11

I'm trying to implement a JSON viewer chrome extension. I already have the Viewer implemented with Vue (http://treedoc.org). Now the problem is how can I can inject the Vue page with Chrome extension content script.

I found this post is very helpful to inject normal javascript file in the extension. But this is with a known javascript file name.

I found this post and this vue-cli-plugin-browser-extension is helpful to use Vue for extension options page and override page, etc, as the entry point for them are HTML files. but the content script entry point is a javascript, to inject a javascript file in the content script, you need to know the exact file name. With the plugin and Webpack, the generated javascript files are appended with hash such as "override.0e5e7d0a.js" which is not known upfront.

Jianwu Chen
  • 5,336
  • 3
  • 30
  • 35

2 Answers2

8

Recently I was developing chrome extension with vuejs and has the same requirement that need to inject whole Vue app to specific web page i.e. google.com, but sadly vue-router doesn't work.

I used this boilerplate to get it working easily with least changes.

Once you created Vue project with this boilerplate then you need to update src/popup/popup.js to inject vue app in web page.

...
const div = document.createElement("div")
document.body.insertBefore(div, document.body.firstChild);

new Vue({
   el: div,
   render: h => { return h(App); }
});

Add vue app as content scripts in manifest.json instead of browser_action or page_action.

"content_scripts": [{
    "matches": ["*://*.google.com/*"],
    "js": [
        "popup/popup.js"
    ],
    "css": ["/popup/popup.css"]
}]

Here vue app will be injected on google.com only. If you want to inject in all web pages then remove matches from content_scripts

P.S.

Managed to vue router working with mode abstract and call router.replace("/") after new Vue({ ..., router })

0

The vue-cli-plugin-browser-extension does not include a hash in the filename of content scripts, so no need to worry.

To add content scripts, configure the plugin's componentOptions.contentScripts, as shown in this example (generated from vue add browser-extension from the root of a Vue CLI project):

// vue.config.js
module.exports = {
  //...
  pluginOptions: {
    browserExtension: {
      componentOptions: {
        background: {
          entry: 'src/background.js'
        },
        contentScripts: {
          entries: {
            'content-script': [
              'src/content-scripts/content-script.js'
            ]
          }
        }
      }
    }
  }
}
tony19
  • 125,647
  • 18
  • 229
  • 307
  • Thanks for the suggestion. However, what I need is to inject a Vue page into the existing content page. So content script needs to update the DOM with the content of a Vue page. The Vue page generated by Webpack includes one entry HTML page and a JS file with hash code in the name. It seems I find a way to do that with iframe. I still experiment with it, will update once I can make it work. – Jianwu Chen Jan 20 '20 at 18:34