Webextension: Variable match pattern for content script

[quote=“NilkasG, post:21, topic:13144”]
Besides being much more readable…[/quote]
To you maybe ; )

AFAICS (though I’ve never used arrow functions) your code is just another way of doing the same thing. I followed the examples here Does your reference to a stack trace imply you think my code is not the problem? (I’ve no experience of stack traces.)

And, on the subsidiary point, you confirm what I thought, but what is the equivalent of request.get()? I’ve looked through all the webextension APIs and can’t find anything. Do I have to use xmlhttprequest in the background script?

Edited to add: I changed the code to match yours. This time I get:
SendMessage error <unavailable>

Do I have to use xmlhttprequest in the background script?

That or maybe the fetch API.

Edited to add: I changed the code to match yours. This time I get:
SendMessage error

Meh. This is a Firefox bug. Maybe you can see it in a different console (Add-on debugger). Otherwise try to log the error.stack.

To you maybe ; )

It surely is a matter of style and I certainly can’t tell you how to code, but:
If you use named promise handlers in more than one case, the code will be a “callback hell”. One of the very points of Promises is to avoid that.
There is absolutely no point in calling new Object().
If you log errors, use console.error(). And generally log the error as an object, don’t concat it with the string.

OK. I appreciate your help.

No excuses, but I learned to program in the '60s - before OOP existed. So I rarely use one statement when three is clearer to me. I’ll plug on …

I learned to program in the '60s

Wow :smiley: I don’t think I have ever met someone who could say that about himself.

So I rarely use one statement when three is clearer to me

Even at the risk that you already know this: I think you would really like async functions.

I finished the webextension version of my Jolla Askbot Unseen Posts addon - it’s described in the OP - a month ago (v2.0.0). I tested it with Fx 53 developer edition. (I don’t intend to release it until it works with Android which needs the options_ui API.) I didn’t try it with the then-current Fx 51.

Since then I updated the existing SDK version to include an embedded webextension to handle the eventual transfer of prefs from simple-prefs to browser.storage.local (v1.1.6). That works in Fx 51 and now in Fx 52.

I just tried the webextension version (v2.0.0) in Fx 52 and it doesn’t work. On the console I get:
Error: prefs is undefined (unknown)

It still works in Fx 53 - and now Fx 54 - but not Fx 52.

‘prefs’ is the name of the object I write to browser.storage.local which contains the options, migrated from simple-prefs. The background script starts with this:

// Start: retrieve the stored prefs
var prefs = new Object() ;
var gettingItem = browser.storage.local.get('prefs') ;
gettingItem.then(onGot, onError) ; 

function onGot(item) {
  if (item['prefs']!=undefined) {
    prefs = item['prefs'] ;
  }
  else {
    prefs['colorS'] = "green" ; 
    prefs['colorU'] = "red" ;
// ... more like that ...
// Store default prefs
  	setting = browser.storage.local.set({prefs}) ;
    setting.then(null, onError);
  }
  mainScript() ;
}

The storage object is also saved in the options.js script but it fails without going there. And as I said, it all works in Fx53/54.

I mentioned the transition version v1.1.6 earlier in case the fact that its embedded webextension will have created the ‘prefs’ object in storage.local in the first place, to be be picked up by the webextension version 2.0.0. I don’t think that causes the problem - the transition from v1.1.6 to v2.0.0 works fine in Fx 53 - and AFAICS v2.0.0 fails without being installed over v1.1.6.

Where it this ‘prefs is undefined’ message from? What does ‘unknown’ instead of a js file/line mean?
I’m guessing that something is wrong with storage.local, or my use of it.

The webextension (v2.0.0) is here:
https://dl.dropboxusercontent.com/u/862480/temp/jolla_together_unseen_posts_2.0.0.xpi

Where it this ‘prefs is undefined’ message from?

I have installed and repetedly started your Extension in FF 52 and each time it reached the mainScript() statement without any exceptions.
Firefox’s debugger really sucks though. I haven’t been able to break on any breakpoints, even though they were defensively passed -.-

What does ‘unknown’ instead of a js file/line mean?

I think that happens if the error is thrown in a different context then the console runs in and the console fails to copy the stack trace information. Where did you get that error from?

I’m guessing that something is wrong with storage.local, or my use of it.

The only mistake I see is he undeclared setting variable, but that’s fine as long as you don’t add a "use strict"; or it clashes with something else.

The addon colours dates on the Jolla Together site and the error message occurs when you load a page from that site. For example if you go to
https://together.jolla.com/questions/
which causes the background script to call content script
jolla_together_unseen_posts_3.user.js
If, on that questions page, you open any question then it calls content script
jolla_together_unseen_posts_1.user.js
and the date/times (eg “asked 7 hours ago”) should initially be red (it shows you haven’t seen it before). If you refresh it should go green. That isn’t working.

The error is in the normal js console, and also in the addon debug console (also ‘unknown’).

Both content scripts use code similar to that which I posted to access the prefs.

Do you see that error message as well?

Nope. No error and it’s working.

Well that’s encouraging. I’ll try with a new installation of Firefox - tomorrow.

Thanks for your help again. You’re welcome to my place in the review queue if you need it :wink:

I don’t think it works that way, but thanks anyway :D

With a new profile and on the same (SuSE linux) build of Fx52 it all seems to work - including the transition from simple prefs to storage.local via the embedded webextension version.

I’ll resume waiting for the Android APIs…

I have the same problem! could you help to solve that problem. I have three files(Background.js and contentscript.js and manifest.json) working on chrome extension. i want to use it on firefox mozilla but it’s not working. how to change the script chrome to firefox extension and how to install it also? Thanks.

I think you should start a new thread. This one is specifically about how to manage host permissions efficiently, while your extension apparently doesn’t run at all.

See also:

thanks a lot!

Le mar. 24 déc. 2019 à 17:55, jscher2000 via Mozilla Discourse notifications@discourse.mozilla.org a écrit :