You need to add the "webNavigation"
permission!
Thanks, yet again!
ā¦
Iāll continue this thread for another issue from converting my addon from SDK to webextensions. Iām testing with Fx 53.0a2 (2017-01-29) (64-bit)
In the SDK addon one of my content scripts sends a message to the background script, which then reads the siteās RSS feed and sends a response. (I couldnāt do that in the content script because the RSS feed has a different origin.) It uses port.emit in both content and background script.
I have converted that to use runtime.sendMessage. The content script how has:
message = [... an array of strings ...] ; // from SDK version code // 'message' now has to be sent in an object var messageObj = new Object() ; messageObj['theMessage'] = message ; var sending = browser.runtime.sendMessage( messageObj ) ; sending.then(handleResponse, handleError); ... /* Message error */ function handleError(error) { console.log(`SendMessage error: ${error}`); } /* Process response from background script to RSS check */ function handleResponse(RSSresponseObj) { ...
This generates an error:
SendMessage error: Error: Constructor Request requires ānewā
In case itās relevant, the background script still has the SDKās request API so that might be implicated, though the error comes from the sendMessage error handler.
Subsidiary question. Itās not clear whether the SDK request API will still work in a webextension. The existence of the old code doesnāt generate an error, but I donāt think itās been executed yet. The documentation says I should use webextensions instead, and I set out today to convert it ā¦ but Iām not clear what API is the equivalent of request.get().
Itās not clear whether the SDK request API will still work in a webextension.
You canāt use anything you needed to get through require()
in the SDK.
You can use this code. Besides being much more readable, it should give you a stack trace to your error:
var sending = browser.runtime.sendMessage({
messageObj: [ /* ... an array of strings ... */ ], // from SDK version code
})
.then(RSSresponseObj => {
// ...
})
.catch(error => console.error('SendMessage error', error));
And since you are using Firefox 52+ anyway, you could even use async
/await
.
[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 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
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 :