service worker registration failed status code 15

16,413 views
Skip to first unread message

Sai iluru

unread,
Aug 8, 2023, 1:15:54 AM8/8/23
to Chromium Extensions
Hi, 

I am unable to make the service worker of my extension active.
Screenshot (117).png

Sai iluru

unread,
Aug 8, 2023, 1:20:48 AM8/8/23
to Chromium Extensions, Sai iluru
Actually I am new to this javascript and this stuff, want have a deep look into how these web extensions work and where they can potentially fail or become problematic to develop and how to fix them. Its a bit hard to learn from web. so want to discuss with people who know that well so that we can create a source that address all doubts clearly and slowly overtime.

wOxxOm

unread,
Aug 8, 2023, 2:52:36 AM8/8/23
to Chromium Extensions, Sai iluru
Code 15 means script evaluation error and judging by the error text below you're using chrome.action.onClicked, in which case you probably didn't add "action": {} to your manifest.json. After you do, click the reload icon for your extension in chrome://extensions UI to apply the changes. Note that the old errors will be still listed there, so you'll have to delete them manually by clicking the "Clear all" button.

Sai iluru

unread,
Aug 8, 2023, 2:04:02 PM8/8/23
to Chromium Extensions, wOxxOm, Sai iluru
Hi @w0xx0m, Thanks I have tried it. The objective is to learn how the web extension files communicate. I want to show the text that is copied from the clipboard into poup.html using storage. 

This is my popup.html.
I also doubt If I can use the script here because once I have tried this I got a security policy error now I couldn't even test as my service worker itself is not up.
<!DOCTYPE html>
<html>
<head>
<title>Clipboard Text</title>
</head>
<body>
<h1>Clipboard Text</h1>
<p id="clipboardText"></p>
<script>
const text = chrome.storage.local.get("clipboardText");
if (text) {
  document.getElementById("clipboardText").innerHTML = text;
}
</script>
</body>
</html>


This is my manifest.json
{
    "name": "My Extension",
    "version": "0.1",
    "manifest_version": 3,
    "permissions": [
      "clipboardRead",
      "storage"
    ],
    "background":{
      "service_worker":"background.js"
    },
    "action": {
          "default_title": "test",
          "default_popup": "popup.html",
          "default_icon":"pic1.png"
        }

  }


This is background.js.
document.addEventListener("copy",function(event){
    console.log(event.clipboardData.getData("plain/text"))
    const text = event.clipboardData.getData("text/plain");
    chrome.storage.local.set(
        {
            "clipboardText": text
        }
   
    );
})


I tried to check the log of background.js in application of console but couldn't find it as it hidden. One more thing I believe I couldn't see it because my service of previous error "Service worker registration failed. Status code: 15" And I am not able to click the "Inspect viewsservice worker (Inactive)" in the extensions page to see console. 

Please feel free to ask any addition info.

Thanks.


wOxxOm

unread,
Aug 8, 2023, 4:21:50 PM8/8/23
to Chromium Extensions, Sai iluru, wOxxOm
There are several problems.

  1. chrome://extensions UI is misleading you by showing old errors without indicating they're old before you changed and fixed your background script. You'll have to account for this poor design and click the "Clear all" button every time you reload the extension.

  2. The background script (the service worker) doesn't have `document` or `window`. This script is generally only necessary to process `chrome` events like chrome.tabs.onUpdated. In this case you don't need the background script at all, simply process DOM events in your visible page e.g. in the action popup.

  3. ManifestV3 extensions can't use inline code in `<script>` element. You should use a separate popup.js file and load it as <script src="popup.js"></script>
The popup runs only when shown, so you'll just read the clipboard right away. The only way to do it inside the popup is by using the deprecated document.execCommand due to https://crbug.com/1337152.

popup.html:

<!DOCTYPE html>
<h1>Clipboard Text</h1>
<textarea id="clipboardText"></textarea>
<script src="popup.js"></script>

popup.js:

const el = document.getElementById('clipboardText');
el.focus();
el.select();
document.execCommand('paste');


If you also want to dynamically update the element while the popup is shown, you'll have to inject a content script that listens to 'copy' event, but that'll be unreliable as it won't see the text copied from the address bar or from another application, so a more reliable approach would be polling the clipboard in popup.js.

popup.html:

<!DOCTYPE html>
<h1>Clipboard Text</h1>
<textarea id="clipboardText"></textarea>
<textarea id="paste" style="opacity:0; width:1px; height:1px; border:none"></textarea>
<script src="popup.js"></script>

popup.js:

paste(true);
setInterval(paste, 250);

function paste(force) {
  const elText = document.getElementById('clipboardText');
  const tmp = document.getElementById('paste');
  tmp.focus();
  tmp.select();
  document.execCommand('paste');
  if (force || elText.value !== tmp.value) {
    elText.value = tmp.value;
  }
}

Reply all
Reply to author
Forward
0 new messages