0

How to call the VueJs object via Chrome Extension?

I'm using the chrome.tabs.executeScript function to try to execute the code inside the active tab.

The idea is to change the values ​​of the fields.

So far I am unsuccessful.

Could anyone help?

My app Main

window.App = new Vue({
  store,
  render: h => h(App)
}).$mount("#app");

Chrome Extension

chrome.tabs.query({url:"http://localhost:8081/portaria/create"}, function(tabs) {

                        chrome.tabs.sendMessage(tabs[0].id, {content: true, body:response.documento}, function(response){

                            chrome.tabs.executeScript( tabs[0].id, {code:"console.log(App)"});
                            chrome.tabs.executeScript( tabs[0].id, {code:"console.log(window.App)"});
                            chrome.tabs.executeScript( tabs[0].id, {code:"window.App.$store.state.form.nfe.codigo = 102"});

                        });
                    });

enter image description here

**** UPDATE *****

Manifest

{
    "name": "Nfe - Nutrane",
    "version": "1.0",
    "manifest_version": 2,
    "content_scripts": [
        {
            "matches": ["<all_urls>"],
            "js": ["content.js"],
            "run_at": "document_start"
        }
    ],
    "browser_action": {
        "default_popup": "popup.html",
        "default_title": "Nfe"
    },
    "background":{
        "scripts": ["background.js"]
    },
    "permissions": ["tabs", "activeTab", "http://*/*", "https://*/*"],
    "web_accessible_resources": [
        "app.js"
      ]
}

Content

 var s = document.createElement('script');
  s.src = chrome.extension.getURL('app.js');
  (document.head||document.documentElement).appendChild(s);
  s.onload = function() {
      s.parentNode.removeChild(s);
  };
console.log(App)

enter image description here

25
  • Your injected code should dive a level deeper, in page context. Commented Jul 11, 2020 at 4:55
  • I think I have part of it. Do I have to load the builded file into the extension folder? Thank you friend. Commented Jul 11, 2020 at 5:14
  • Yes, you can either use a separate js script or use a string. The answer I've linked contains all examples. Commented Jul 11, 2020 at 5:25
  • I tried to use method 1 but I can't get it to work. Commented Jul 11, 2020 at 5:49
  • Add the new code and how you call it to the question. Commented Jul 11, 2020 at 5:53

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.