0

I have a ploblem with making Chrome Extention.I got below error in popup.js file:

"Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist." also I live in Iran and I have the internet problem and can not access google documents!!!

please help me to solve this problem. Thanks

this is my code:

1.manifest.json

{
  "name": "Hamid Test",
  "version": "1.0",
  "manifest_version": 3,
  "author": "Hamid Solat",
  "description": "This is a test",
  "icons":{
    "128":"/assets/icons/icon.png"
    },
  "background.service_worker": {
    "scripts": ["assets/js/background.js"],
    "persistent": false
  },
  
  "action": {
    "default_icon": "/assets/icons/icon.png",
    "default_popup": "popup.html",
    "default_title": "Hamid Test"
  },
  "content_security_policy": {}
  ,
  "permissions": [
    "activeTab",
    "tabs",
    "storage",
    "background",
    "webNavigation"
  ]
}

2.popup.html

<!doctype html>
<html>
  <head>
    <title>Hamid Test</title>
    <link rel="stylesheet" href="assets/css/popup.css">
  </head>
  <body>
    <div class="container">
      <h1>Hamid Test</h1>
      <button id="injectMessage">Send</button>
      <div id="validation"></div>
    </div>
    <script type="text/javascript" src="assets/js/popup.js"></script>
  </body>
</html>

3.popup.js

chrome.storage.sync.clear();
let button = document.getElementById("injectMessage");
let validation = document.getElementById("validation");
sleep = async (ms)=>{ return new Promise(resolve => setTimeout(resolve, ms)); }

button.addEventListener("click", async ()=> {
    chrome.runtime.sendMessage({"manageMischief": true});
});

4.bacground.js

chrome.runtime.onMessage.addListener((message, sender)=> {
    if(!message.manageMischief) return;
    else{chrome.tabs.executeScript({
        file: 'assets/js/inject.js'
    }); }
});

It's the first time I make the chrome extention...

I also try this in popup.js: button.addEventListener("click", async ()=> { alert("OK"); });

it worked.... but my problem is in this line:

chrome.runtime.sendMessage({"manageMischief": true});

hamidsolat
  • 11
  • 1
  • 1) Remove `background.service_worker` and declare it properly as shown in the documentation, 2) executeScript is now in chrome.scripting API which uses a separate permission and a different invocation syntax. 3) Note that for the posted code you don't need the background script because you can call executeScript in your popup script ([examples](https://stackoverflow.com/q/4532236)). – wOxxOm Dec 27 '22 at 16:17
  • "I live in Iran and I have the internet problem and can not access google documents" -- 1) If you can access Github, you can create a local copy of https://developer.chrome.com/ by following the instructions at https://github.com/GoogleChrome/developer.chrome.com/ -- 2) I remember giving someone from Iran this information before, but I can't find the comment. Maybe it was deleted because it counts as circumventing sanctions or something like that? If that's the case, I'd like to know about it. – Thomas Mueller Dec 27 '22 at 16:20
  • thank Thomas, I'm try to understand V3, I guessed this problem is due to I copied from v2 source code. If you had an example of V3, I could've solved it faster – hamidsolat Dec 27 '22 at 16:45

0 Answers0