Need help to put content script right way
See original GitHub issueHi 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:
- Created 6 years ago
- Comments:11 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Oops! Yes, that were the problem. Sorry for bordering your time…
It’s because the line
"matches": ["*://*.google.com/*"]
of your manifest tells Chrome to inject yourcontent.js
only into Google’s sites