adambullmer / vue-cli-plugin-browser-extension

Browser extension development plugin for vue-cli 3.0
GNU Lesser General Public License v3.0
426 stars 76 forks source link

Content and Background Scripts are not executing in dev mode #63

Open theharshin opened 4 years ago

theharshin commented 4 years ago

I've installed the plugin (v0.22.5) on the fresh setup of Vue CLI and the serve command builds the files successfully. The extension loads and displays [ WER: Connected to Extension Hot Reloader ] but code in content-script or background is not being executed. Surprisingly, when I build the extension and load it, everything works fine! Something to do it Extension Hot Reloader?

theharshin commented 4 years ago

I've tried to use v0.19.0 with the same setup and everything works fine! 🤔

theharshin commented 4 years ago
image

After removing this check, everything works fine.

rrfaria commented 4 years ago

I opened a PR suggesting this change https://github.com/adambullmer/vue-cli-plugin-browser-extension/pull/64

adambullmer commented 4 years ago

Can you share you vue.config.js file and your src/manifest.json? Also how are you trying to invoke your content scripts and background scripts?

rrfaria commented 4 years ago

I found out the problem is not in the code please discard this PR.

the plugin even you are selecting content-script in options, it doesn't add on src/manifest.json and only vue.config.js is right

"content_scripts": [
    {
      "matches": ["*://*/*"],
      "js": ["js/content-script.js"]
    }
  ],
adambullmer commented 4 years ago

Correct. The snippet you added automatically injects a content script into every page. It is my opinion that this behavior should not be the default scaffolding behavior of browser extensions, as most developers will probably never circle back and change this to a narrower list if they didn't need it in the first place.

That being said, if this continues to be a pain point, then this means there is a difference in expectations of this plugin between myself and the community, or a lapse in documentation around this particular feature. Probably a good first step is to highlight this fact in the readme and see if there continues to be issues around expecting automatically injected content scripts on every page.

rrfaria commented 4 years ago

@adambullmer I believe the misunderstood is happen because when you are creating your first extension there are some options you can choose and when you select "content-script" between them we were expecting no need to setup more things to run it.

sorry for that.

One thing I would like to say is your work is amazing it is helping a lot.

JasonLearmouth commented 4 years ago

Hello @adambullmer, I'm having a similar issue where my background.js code is not running. I have the default content (generated by your extension) in there, so I was expecting the content script to log a message in the web page console, and the background script to log a message to the background console. Neither of these is happening.

Looking at the conversation above, it seems like I need to add some config to get it working, but I don't have enough experience to know what to add or where to add it.

Can you clarify the steps required to get background and content scripts working with the out-of-the-box template?

adambullmer commented 4 years ago

I think I finally stumbled on the root cause of content scripts and background scripts not executing, and should be released in 0.23.1. It looks like vue added in some optimizations in development mode, but only if your project were using a more recent version of vue-cli-service, and would explain why this was a little harder to track down.

@JasonLearmouth concerning your troubles, I think there is a bug in the scaffolding where the incorrect listener is being applied on the background file when also scaffolding with a popup (the default 2 options). This should be fixed in 0.23.2. Depending on how much work you've put into your project I may recommend just rescaffolding with vue invoke browser-extension once you've updated to the latest version. Otherwise, you can replace the top line of your backgound.js file with this:

browser.runtime.onMessage.addListener(function (request, sender, sendResponse) {
doutatsu commented 4 years ago

First time building an extension as well and trying to figure out why the default background.js doesn't execute. I read through the thread here, but still not really sure why it's not working :thinking: Any help would be appreciated