Webextension firefox call remote background.js file possible?


(Leonardo Sedevcic) #1

i need to change this in my webextension: i have a huge database stored in 6 background.js files actually stored inside the xpi (bad idea), it is possible to call my differents background.js files stored on a server & call the files from an url ?

what i need is just to call external backgrounds.js files, actually my extension works perfectly but backgrounds.js files stored inside the xpi is a bad idea.

what i want is the user can never see the backgrounds.js files & content,

it is possible?

thanks to all.


(Emmanuel Sellier) #2

How huge is your DB? You may consider download resources and use local storage.
But these resources should not be remote scripts (at least if you want to publish your extension on AMO).

No you can’t hide the code… You can only minimize and obfuscate the code.


(Leonardo Sedevcic) #3

my DB is 7mb at the moment, divided in 6 background.js 1mb each, i update my database almost every week.

ok, how can i do a download ressources for the 6 background.js files?

my add-on is allready working with firefox quantum and hosted in mozzilla add-ons

i want to do those changes because i got a very bad review :smiley: ( my first) so i know, keeping the background.js files with the blocked urls inside the xpi is a bad idea, thats why im looking for a better solution. what i want is just to keep the users happy (advancced users who look into the code :D), and feel secure with my add-on.


(erosman) #4

Getting a Remote JS file is not allowed in add-ons on Mozilla add-ons.

You can create a plain DB file and get it remotely but that would take a fair amount of time which will stop the extension working. In fact. it is not better than having the files internally.

I am familiar with your add-on. The best way is to create a text file just for the patterns, then read them on start up and create an array from them. Then use a single webRequest.onBeforeRequest.addListener for all of them.

There are many ways to optimise the code.

An example would be to create patterns and pass them directly to webRequest.onBeforeRequest.addListener

Example:

// a separate file for the patterns

pat1
pat2
pat3

// Get the file, read it into text… create an array
let pattern = fileText.split(/[\r\n]'/);

Now you have an array of patterns that you can pass to webRequest.onBeforeRequest.addListener

Note they must be patterns which makes it easier

you can use this: *://*.bongacams.xxx/*

Instead of all these:

"mk.bongacams.xxx": false,
"it.bongacams.xxx": false,
"hr.bongacams.xxx": false,
"ro.bongacams.xxx": false,
"sk.bongacams.xxx": false,
"hu.bongacams.xxx": false,
"gr.bongacams.xxx": false,
...
...

Then the listener

browser.webRequest.onBeforeRequest.addListener(
  cancel,
  {urls: [pattern],
  ['blocking']
);

(Leonardo Sedevcic) #5

Do you think you can help me improving my add-on the background.js file??

i know, you did a lot of review of my add-on so for sure you are familliar with it :smiley: :smiley:

thanks


(erosman) #6

I can make suggestions.

I had another look… and using pattern may become more complicated.

What would be the size of a text file with all of domain only?

eg:

exoclick.com
googleads.g.doubleclick.net
ad.doubleclick.net
ads.rubiconproject.com
www.4tube.com
www.5ilthy.com
www.alotporn.com
www.alphaporno.com
www.angrymovs.com
www.anysex.com
www.assfilled.com
....
....

(Leonardo Sedevcic) #7

actually if i put all the urls inside a txt file, if i remove everything exepted the urls, its amost 5mb, and i still keep updating my database background.js files :smiley: not sure if pattern can work in my case, because of the urls, who are really complex in some casses…


(erosman) #8

5mb is not too bad.

Create a text file with it, name it something.dat

Then you can use fetch() on background script to get it and add listeners.

I do a similar thing in my FoxyTab addon (the DB will be 4mb in next version, but was 6mb in the current released version)


(Leonardo Sedevcic) #9

im really interrested to change my backgrounds.js files into a *.dat file but dont know how, do i have to chaneg everything inside it?

i supose its impossible to see the content of a dat file for a regular user trough the browser dev tool?


(erosman) #10

This is a plain text file with .dat extension which is not the same as a binary .dat file.

Binary file would be much smaller (probably about 1.5-2mb) but more complicated to read and write to and update.

Just make a normal .txt file with those and then rename it .dat, although .txt should be OK too.

There is limit on file size on Mozilla if they are JS or JSON .


(Leonardo Sedevcic) #11

inside the .dat file do i have to rename my urls

badurl.com”: false,
newbadurl.com”: false,

to

badurl.com”,
newbadurl.com”,

or i have to just keep urls like:

://.badurl.com/,
://.newbadurl.com/
,


(erosman) #12

Just keep the domain names, no comma, no quotes, no spaces, only a new line after each (which keeps the size to minimum)

Example

badurl.com
newbadurl.com
exoclick.com
googleads.g.doubleclick.net
ad.doubleclick.net

Depending on what they are (i.e. are they all host names?) the match can be made in different ways.

BTW, since you are only looking at http/https then there is no need for "<all_urls>" and urls: ["http://*/*", "https://*/*"] should be fine.


(Leonardo Sedevcic) #13

so if i understand well :smiley: (hope so )

i rename the" background.js" file to “newname.dat”, inside i change everything by plain text urls:

url01.com
url02.com
url03.com

then i replace:

{urls: ["<all_urls>"]},[“blocking”]);})();

by

{urls: [“http:///”, “https:///”]},[“blocking”]);})();

so my dat file is goign to look like this:

(function() {
var AUTHORIZED_DOMAINS= {

url01.com
url02.com
url03.com

};
function extract_domain(url) {
var matches = url.match(/^https?://([^/?#]+)(?:[/?#]|$)/i);
return matches[1];
}

browser.webRequest.onBeforeRequest.addListener(function(details) {

var domain = extract_domain(details.url);

return { cancel: AUTHORIZED_DOMAINS[domain]===false };
}, {urls: [“http:///”, “https:///”]},[“blocking”]);})();

im right? :smiley:


(erosman) #14

No… those are the first steps…

Addon still needs to read the .dat file and turns it into an object which needs a separate code (not presently in your addon).

Then the match system for the webRequest.onBeforeRequest.addListener has to change to find the right result.

It is a simple bit of code which requires a bit of testing to get the best performance. I am travelling and not at my base location so I have limited resources. I can look into in maybe in couple of weeks when I get back to my base.

BTW, disabling F12 is very annoying to many users and for example I use it all the time and if an addon disables general browser functions, I would uninstall it immediately. It doesn’t affect your add-on at all and it shouldn’t be there. I dont know why you have it.

It also adds to the resource usage since it is injected into every page.


(Leonardo Sedevcic) #15

yeah i have to remove the f12 :smiley:
thanks a lot for your help :smiley:

removed in the current version 1.8.84 :smiley:


(Leonardo Sedevcic) #16

Happy christmass & new year :smiley: do you think soon you can help me to achive that?

i was thinking how can i disable clicks href on:

function extract_domain(url) {
var matches = url.match(/^https?://([^/?#]+)(?:[/?#]|$)/i);
return matches[1];
}

chrome.webRequest.onBeforeRequest.addListener(function(details) {

var domain = extract_domain(details.url);

return { cancel: BURL[domain]===false };

}, {urls: [“http:///”, “https:///”]},[“blocking”]);})();

i tried to add
return {redirectUrl: ‘javascript:void(0)’};

but then nothing more / urls works on my browser :smiley:


(erosman) #17

Lets fix the database issue first.

I will try to write a code for you now that I am back to my base.


(Leonardo Sedevcic) #18

hihi :smiley: thanks so much :smiley:

i spent all this week thinking how to protect or hide the databse because i dont want the user to be able to acces easily to it.

my brain hurt :smiley:


(erosman) #19

That is a different matter.
My suggestion was to get rid of all those background_*.js
It wont hide the database.


(Leonardo Sedevcic) #20

Yeah i know,
i spent lot of time reading forums, apparently we can offuscate the files but not hidde them from the dev console (F12) :s

but if we get this right one file with the db instead of lots of background.js files :smiley: it’s already a success