Webextension firefox call remote background.js file possible?


(erosman) #21

I have not tested the code so you have to test it…

1- Create a plain text file for the database and name it database.dat

2- Put the domains only in above file, without any extra path or characters. One domain per line.
example:

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

Do not add paths as it wont work in this code:
www.example.com/somepath/other

3- Change the manifest.json

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

4- Create a single background.js
Paste the following only in background.js

'use strict';

let db = []; // session Global

// ----- parse & cache the database data
fetch('database.dat')
  .then(response => response.text())
  .then(text => { db = text.trim().split(/[\r\n]+/); })
  .catch(error => console.log(error));

chrome.webRequest.onBeforeRequest.addListener( details => {

    let url = new URL(details.url);
    return { cancel: url && url.hostname && db.includes(url.hostname) };
  }, 
  {urls: ["http://*/*", "https://*/*"]},
  ["blocking"]
);

Let me know how it works out.


(Leonardo Sedevcic) #22

i did exactly that but urls are no more blocked :s

and if i look with F12 i get lots of :

Failed to load resource: net::ERR_BLOCKED_BY_CLIENT

Uncaught (in promise) TypeError: Cannot set property ‘innerHTML’ of null


(erosman) #23

Where is the innerHTML?
There is no innerHTML in my code.


(Leonardo Sedevcic) #24

thats the message who appears when i reach some specific xxx blocked website it doesnt happens all the time, in almost every blocked website i get something like this with ( F12 )

but the urls / websites are not blocked :s


(erosman) #25

Those are from NoScript, aren’t they?

They have nothing to do with your add-on.

Get me a copy of what you have done and I will see.


(Leonardo Sedevcic) #26

i sended you a message with the url :smiley:


(erosman) #27

Oops… There was a typo… I fixed it… try the above code again

Also… you do not need "unlimitedStorage" permission and you should remove it as unnecessary permissions is cause for rejection.

That is only needed if the addon is saving large amount of data to storage (local, IndexedDB, etc) which your addon doesn’t.


(Leonardo Sedevcic) #28

Thanks it works :smiley: urls are blocked again

do yo uthink its possible to do a silent click event disable?

when user try to click one of the urls present in the database click is not working, like under firefox

i was thinking to something like:

javascript:void(0)


(erosman) #29

Possible, yes
Advisable, no

Links and clicks are part of page content while the add-on is running in browser content (background).

Adding such a blocker to links means injecting script into every page, and check against the (very large) database. That would create a HUGE overhead (memory and resource usage) and slows down Firefox.
I dont advise that at all. In fact add-ons that affect Firefox performance get rejected for that reason. :wink:

Did you get the rar file I sent you?


(Leonardo Sedevcic) #30

yes i got it, it works perfeclty :smiley: but under chroem i get the error message:

ERR_BLOCKED_BY_CLIENT disabled by add-on.

this is because of the cancel function, it’s possible to do a redirection to google instead?

thanks for all the help :smiley:


(erosman) #31

It is possible but if you recall form my reviews in the past… troublesome.

1- That allows Google to track users and check if the pages that users visit has porn elements
2- A page with many block-able items will result in an ugly mess
3- Creates extra bandwidth for the users to load Google every time needlessly
4- Addon name and function has to be changed to say, it redirects porn, not stops it as stop means no more loading (not a redirect)

I wouldn’t advise it.

Where does the Chrome error message appear? In the console? It is not an error message. It is info.

It also seems it is coming from ADB not Chrome


(Leonardo Sedevcic) #32

Thanks for all yeah i think that too Adblock is what gives me that error messages infos.

so if redirect is no ok, maybe its possible to show a personal message instead of that cancel text? :smiley:


(erosman) #33

That is possible… but will look ugly within a page and causes errors.

It the blocked domain was the tab URL, a message would be OK but that is not the case.
There are images that will be blocked and a personal message in IMG source will cause an error since it is not really a image source.

There are other elements such as objects, iframes, etc within a page that will all cause an error.

It is possible to separate many of them but that requires a lot more processing (and code).

Script/Ad blocker works the same way and it would be annoying to get a notice of some sort every time something is blocked.


(Leonardo Sedevcic) #34

can i change the name file database.dat to dburl30122017.dat ?


(erosman) #35

Sure… but I dont advise it as it affects the review process without much gain.

Addon with date/version based JS files will get rejected as it creates a lot of needless work comparing it to the previous version.

You will then have to change the background.js code every time with the date.

Date based file names are really ugly. I have a database in my addon and I update it. I know which version it is and for the users, it is the version of the addon version. That is all that is needed.

addon v1.5 … has the DB for version 1.5
addon v1.6 … has the DB for version 1.6

It doesnt need an exact date and user wont see the dburl30122017.dat

That is only my opinion.


(erosman) #36

Did you remove the “browser_style” intentionally?
I thought it looked nicer with it but it is up to you.

   "browser_action":{  
      "browser_style": true,
      "default_icon":"icon.png",
      "default_popup":"popup.html"
   },

(Leonardo Sedevcic) #37

Ok so im goign to give a easier name :smiley: something like dburl1.8 instead of changing the date name everytime for the dat file and manifest :smiley:
i saw it now i forgot to add “browser_style”: true, for the firefox version :smiley: next update.

in my website i have a little problem maybe you can help too :smiley:

when teh visitor click on teh menu he get an url like this: http://stop-it.be/#section-about

i want the url to be allways http://stop-it.be/ id dont need the /#section-about

inside my html i have
li class=“nav-item " a class=“nav-link” href=”#section-about" style=“font-weight:bold;” >About</li


(erosman) #38

That is not possible with HTML since in order to scroll down, there must be an anchor and that is the #section-about

It is possibly to use JavaScript to scroll eg to add listeners to the menu items and then scroll to the location.


(Leonardo Sedevcic) #39

and for the cancel it’s possible to do a redirection to an html page if the url match?


(erosman) #40

Here is a quick code… not tested… just wrote it as an example:

change each menu item from:
<li class="nav-item "><a class="nav-link" href="#section-about" style="font-weight:bold;">About</a></li>

to:
<li class="nav-item" data-ref="#section-about">About</li>

Add JavaScript

// the ES6 spread not needed for Firefox, added for Chrome and others
for (let li of [...document.querySelectorAll('#probootstrap-navbar li')]) {
  
  li.addEventListener('click', function() {
    
    let node = document.querySelector(this.dataset.ref);
    if (node) { node.parentNode.parentNode.scrollTop = node.offsetTop; }
  });
} 

What cancel?