admin管理员组

文章数量:1287913

I'm trying to implement a JSON viewer chrome extension. I already have the Viewer implemented with Vue (). 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.

I'm trying to implement a JSON viewer chrome extension. I already have the Viewer implemented with Vue (http://treedoc). 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.

Share Improve this question asked Jan 20, 2020 at 2:04 Jianwu ChenJianwu Chen 6,0333 gold badges35 silver badges39 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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., 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./*"],
    "js": [
        "popup/popup.js"
    ],
    "css": ["/popup/popup.css"]
}]

Here vue app will be injected on google. 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 })

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 ponentOptions.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: {
      ponentOptions: {
        background: {
          entry: 'src/background.js'
        },
        contentScripts: {
          entries: {
            'content-script': [
              'src/content-scripts/content-script.js'
            ]
          }
        }
      }
    }
  }
}

本文标签: javascriptIn Chrome Extensionhow to use content script to inject a Vue pageStack Overflow