question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Need help to put content script right way

See original GitHub issue

Hi I try to use content script, but it’s seem to be not working.

What I do is:

Create new content.js

chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
  // If the received message has the expected format...
  console.log('rn')
  if (msg.text === 'report_back') {
    // Call the specified callback, passing
    // the web-page's DOM content as argument
    sendResponse('Hello');
  }
});

add it to manifest

{
  "name": "movie-extension",
  "description": "A Vue.js web extension",
  "version": "1.0.0",
  "manifest_version": 2,
  "icons": {
    "48": "icons/icon_48.png",
    "128": "icons/icon_128.png"
  },
  "permissions": [
    "activeTab"
  ],
  "browser_action": {
    "default_title": "movie-extension",
    "default_popup": "popup/popup.html"
  },
  "content_scripts": [{
    "matches": ["*://*.google.com/*"],
    "js": ["content.js"]
  }],
  "background": {
    "scripts": [
      "background.js"
    ]
  }
}

add file to the CopyWebpackPlugin

new CopyWebpackPlugin([
      {from: 'icons', to: 'icons', ignore: ['icon.xcf']},
      {from: 'popup/popup.html', to: 'popup/popup.html'},
      {from: 'manifest.json', to: 'manifest.json'},
      {from: 'content.js', to: 'content.js'}
    ])

Try to use it in the vue component

<template>
  <div>
    <p>Hello world!</p>
    <button @click="test">Test</button>
  </div>
</template>

<script>
  export default {
    data () {
      return { }
    },
    methods: {
      test () {
        chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
          chrome.tabs.sendMessage(tabs[0].id, {text: 'report_back'}, function(response) {
            console.log(response);
          });
        });
      }
    }
  }
</script>

But I got undefined in the console.log 😦

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
kieusonlamcommented, Mar 5, 2018

Oops! Yes, that were the problem. Sorry for bordering your time…

0reactions
Kocalcommented, Mar 5, 2018

It’s because the line "matches": ["*://*.google.com/*"] of your manifest tells Chrome to inject your content.js only into Google’s sites

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chrome Extensions content scripts
An explanation of content scripts and how to use them in your Chrome Extension.
Read more >
Content scripts - Mozilla - MDN Web Docs
A content script is a part of your extension that runs in the context of a particular web page (as opposed to background...
Read more >
Chrome Extensions: Content Scripts - Programming with Text
In this video, I cover " content scripts " for chrome extensions. The content script is a JavaScript file that runs in the...
Read more >
How to Inject Content with a Chrome Extension - MV2 & MV3
https://rustyextensions.com/academy Preorder the Chrome Extension Academy today!Hey! In this video we look the different ways you can inject ...
Read more >
Chrome extension content script re-injection after upgrade or ...
Install /upgrade. The install method is to simply iterate through all tabs in all windows, and inject some scripts programmatically into tabs with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found