0

I have tried to apply solutions from other topics related to issues with content script but they doesn't worked. Source in dev tools doesn't see content script from that extension. I recive an error from console of extension

Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.

Looks like extension doesn't see content.js at all.

So, I my case - content.js doesn't respont and work. Did I missed something in manifest.json?

manifest.json

    "name": "Notification Extension",
    "version": "1.0",
    "description": "Build an Extension!",
    "permissions": [
        "storage",
        "notifications",
        "alarms",
        "activeTab",
        "tabs",
        "http://*/*",
        "https://*/*"
    ],
    "background": {
      "scripts": ["background.js"],
      "persistent": false
    },
    "browser_action": {
      "default_popup": "popup.html"
    },
    "content_scripts": [
      {
        "matches": [
          "http://*/*",
          "https://*/*"
        ],
        "js": ["content.js"],
        "run_at": "document_end"
      }
    ],
    "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
    "manifest_version": 2
  }

content.js

console.log('content.js is working');

chrome.runtime.onMessage.addListener(
    (request, sender, sendResponse) => {
        if(request.createAlarm){

            console.log('alarm senedet to content.js')
        }
    }
)

popup.js

chrome.tabs.query({active:true, currentWindow:true}, (tabs) => {
    chrome.tabs.sendMessage(tabs[0].id, {createAlarm: true, message: 'sending to extension'}, (response) => {
        console.log('message arrived');
    })
})

Thanks for your help!

elBrambore
  • 110
  • 2
  • 8
  • The error message syas there was no content script running in that tab so it's either a chrome:// tab or you need to [reinject content scripts explicitly](/a/11598753) after reloading or re-enabling the extension. Otherwse you'll have to reload the tabs manually to let Chrome run the content scripts. – wOxxOm Mar 11 '20 at 04:16

2 Answers2

1

I believe you can only use chrome.tabs.sendMessage to send messages from background scripts to content scripts.

Check this answer for a working solution: https://stackoverflow.com/a/47313968/3862289

ata
  • 3,398
  • 5
  • 20
  • 31
Gergely Szabo
  • 870
  • 6
  • 15
  • Thanks for your replay. From what I know it's possible to send messages between all extension's parts (regard to https://developer.chrome.com/extensions/messaging). But despite that, console.log from content.js doesn't log anything in website console. So it looks like extension doesn't see content.js. I think it can be something with my manifest, but I cant see anything. – elBrambore Mar 10 '20 at 22:11
  • Ohh, so your content script does not run at all? Then the question is not really about messaging ( I think you will have to fix that part too). You did not say what URL you tried this on, thought that should match everything according to https://developer.chrome.com/extensions/match_patterns . Can you tell us what URL you tried this on? Can yo try using instead? – Gergely Szabo Mar 10 '20 at 22:26
0

So, finally the problem was "new card" on which I tried to test extension. It should be any website. My bad. But I want to thank you for your help! Sorry one more tine for bothering. Best regards!

elBrambore
  • 110
  • 2
  • 8