error when using background.js on extension

3,486 views
Skip to first unread message

Gordon Stewart

unread,
Dec 4, 2022, 3:39:18 AM12/4/22
to Chromium Extensions
Hi

I'm fairly new to creating extensions.

So far I've spent 3-4 days/week, and so far I've got it to display one thing on all URLs, but another information on a specified URL that matches.


in my manifest.json file i have:-

=========================

 "content_scripts":[
    {
      "matches":["https://www.myURL.com"],
      "js":["content.js"]
    }
   ],

 "action": {
    "default_popup": "hello.html",
    "default_icon": "hello_extensions.png"
  },

  "permissions":["tabs"]

"background": {
  "scripts": ["background/background.js"]
}


=========================

Now THIS does work, and the content.js file is going.. if it is on the specified URL.
hello.html displays on all other URLS.

PROBLEM:

when I'm adding my background.js file information to manifest, Manifest doesn't work.

ERROR;

Manifest is not valid JSON. Line: 22, column: 1, Syntax error.
LINE:  "background": {

Can anyone assist with this?

Also - Once I get background going, is there a small tutorial on

a) loop through all the files being downloaded by the user.

b) if it is a json file, loop through the file for specified text.

I'm used to PHP, so these commands may be useful to me:  

- preg_match,  and - extract small information from LONG lines of text.

- save / store (and view) information into the app's memory (user's PC?)

Any guidance appreciated.

Gordon Stewart

unread,
Dec 4, 2022, 3:58:01 AM12/4/22
to Chromium Extensions, Gordon Stewart
changed my background to:

 "background": {
    "service_worker": "background/background.js"
  }

But getting error:

Service worker registration failed. Status code: 15

I've seen on one post/google search that service_worker ONLY works on http: ?? is this correct?
How do I use a background task/script without https?

Jackie Han

unread,
Dec 4, 2022, 4:12:28 AM12/4/22
to Gordon Stewart, Chromium Extensions
I remember there is a bug. You need to put the service worker at the root directory, not sub directory.
If it still doesn't work, check your code.

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/e9b03220-3627-46a3-a385-3f7d8921f395n%40chromium.org.

Christian Mariño Alvarez

unread,
Dec 4, 2022, 4:21:19 AM12/4/22
to Gordon Stewart, Chromium Extensions
And the end of the permissions line you need put ,

"permissions": ["tabs"],



--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.

gordo...@gmail.com

unread,
Dec 4, 2022, 5:21:35 AM12/4/22
to Christian Mariño Alvarez, Chromium Extensions
Hmm.

Is there a good 'background' service task tutorial with *ZERO* knowledge/assumptions that they expect someone to know? 

Just insert/paste and it will work - without needing any un-mentioned scripts.. but a assume you know to add those unmentioned scripts.

I've copied-pasted a dozen scripts into my background.js file (one at a time) and they all produce errors 

my current background - tells me there is an error in the script, but doesn't tell me what error (i click the big red "error' button - Nothing & nothing in the console)...


Is there a small background script that just displays the current time every second - into the popup  so I know it it's working (as a service)...
popup is content.js

content.js :-
-----------------------
chrome.tabs.query({"active": true, "lastFocusedWindow": true}, function (tabs) {
    tabURL = tabs[0].url;
    console.log("URL from get-url.js", tabURL);

let pattern = /forge/;

if(pattern.test(tabURL))
     {

document.write("Welcome to my site");

document.write("<br />URL: ",tabURL);

var allElements = document.getElementsByTagName("*");
var allIds = [];
for (var i = 0, n = allElements.length; i < n; ++i) {
  var el = allElements[i];
  if (el.id) { allIds.push(el.id);
document.write(allIds);
  }
     }



}

});
-----------------------


(the tagname thing doesn't work - but it does show the URL)







--

Gordon.

wOxxOm

unread,
Dec 4, 2022, 9:13:26 AM12/4/22
to Chromium Extensions, gordo...@gmail.com, Chromium Extensions, christi...@gmail.com
> Is there a good 'background' service task tutorial

Judging by the scraps of info in this thread you probably don't need the background script at all. Describe the goal you want to achieve in more detail - it's quite possible everything you need is a content script and a script in the popup.

>  You need to put the service worker at the root directory, not sub directory.

It was fixed in Chrome 93.

Jackie Han

unread,
Dec 4, 2022, 9:45:20 AM12/4/22
to wOxxOm, Chromium Extensions, gordo...@gmail.com, christi...@gmail.com
It was fixed in Chrome 93.

Yay, cheers

gordo...@gmail.com

unread,
Dec 4, 2022, 2:43:45 PM12/4/22
to Chromium Extensions
On Mon, 5 Dec 2022 at 03:13, wOxxOm <wox...@gmail.com> wrote:
> Is there a good 'background' service task tutorial

Judging by the scraps of info in this thread you probably don't need the background script at all. Describe the goal you want to achieve in more detail - it's quite possible everything you need is a content script and a script in the popup.


Here is a summary of my script

1) If it matches a URL - Show 1 HTML output & does something (script). if a user is on another URL/website - show another HTML output (do nothing).

this is WORKING.

2) for the "do something" script,  I want it to scan each file the user downloads - If its a JSON file

:- scan the file for a certain keyword & extract text. - 3 or 4 pieces of information. (some kind of preg_match - similar to PHP)
:- save the text "somewhere" (local directory)

:- once I have that, I can then do something with the saved data - post to an online form ($_POST method) when pressing a button
(saved data will be under 0.01% of the total volume of information received)



I'm thinking - of only 'scanning' the pages after pressing a button - no need for continuous scans.
- I guess we can scan pages after the page has loaded?


the problem is any tutorials I find, do other things but don't 'scan' pages. 

 
--

Gordon.

wOxxOm

unread,
Dec 5, 2022, 9:21:22 AM12/5/22
to Chromium Extensions, gordo...@gmail.com
>   I want it to scan each file the user downloads

There's no API to get the contents of a downloaded file. There's only chrome.downloads.onCreated API event that provides some metadata. Normally extensions need to re-download the URL using `fetch()` to get the contents, but since you're a python person a better alternative may be to register a python script using nativeMessaging API (there are many examples around so it should be easy to implement) and send a message to it when the download completes so that the python script can do the actual analysis of the already downloaded local file.

wOxxOm

unread,
Dec 5, 2022, 9:23:39 AM12/5/22
to Chromium Extensions, wOxxOm, gordo...@gmail.com
Correction: you're using PHP, not python, but the approach is the same although finding an example may be harder for PHP so it may be easier adapting from a python example.
Reply all
Reply to author
Forward
0 new messages