I have a content script that will do some action on a facebok page. I want that the script is executed only if the user click on the chrome extension icon. I'm trying with this code but it will not work as expected. I'm using vue
manifest
{
"manifest_version": 2,
"name": "__MSG_extName__",
"version": "1.0.0",
"description": "",
"default_locale": "en",
"permissions": [
"activeTab"
],
"icons": {
"16": "icons/16.png",
"48": "icons/48.png",
"128": "icons/128.png"
},
"background": {
"scripts": [
"js/background.js"
],
"persistent": false
},
"content_scripts": [
{
"matches": ["https://www.facebook.com/*"],
"js": ["js/content-script.js"]
}
],
"browser_action": {
"default_popup": "popup.html",
"default_title": "__MSG_extName__",
"default_icon": {
"19": "icons/19.png",
"38": "icons/38.png"
}
}
}
content-script (this code will always execute)
const storiesDiv = document.getElementsByClassName('_7h4p');
// debug only
//console.log(storiesDiv);
//
const featuredStories = [];
storiesDiv.forEach( (el) => {
if( el.dataset.onkeypress ){
featuredStories.push(el.dataset.onkeypress);
}
});
const stories = featuredStories.filter( (el, index) => {
if( index != 0 ){
return el;
}
});
stories.forEach( (el) => {
var item = JSON.parse(el);
console.log(item[0].a[0].preloadImageURIs[0]);
});
vue popup.html component (this code will not execute)
<script>
export default {
name: "Popup",
data() {
return {
isLoaded: false,
images: null
}
},
mounted() {
//browser.runtime.sendMessage({});
this.getFeaturedStories();
},
computed: {
defaultText() {
return browser.i18n.getMessage("extName");
}
},
methods: {
getFeaturedStories() {
const storiesDiv = document.getElementsByClassName('_7h4p');
// debug only
//console.log(storiesDiv);
//
const featuredStories = [];
storiesDiv.forEach( (el) => {
if( el.dataset.onkeypress ){
featuredStories.push(el.dataset.onkeypress);
}
});
const stories = featuredStories.filter( (el, index) => {
if( index != 0 ){
return el;
}
});
stories.forEach( (el) => {
var item = JSON.parse(el);
console.log(item[0].a[0].preloadImageURIs[0]);
this.$set(this, 'images', item[0].a[0].preloadImageURIs[0]);
});
}
}
};
</script>
background script (this code will not run ?)
browser.browserAction.onClicked.addListener( (tab) => {
console.log(tab);
browser.tabs.executeScript({
file: "content-script.js"
});
});