prevent extension script from running multiple times using XPCOM observers Topic is solved

Add-ons for Pale Moon and other applications
General discussion, compatibility, contributed extensions, themes, plugins, and more.

Moderators: FranklinDM, Lootyhoof

BZZZZ

prevent extension script from running multiple times using XPCOM observers

Unread post by BZZZZ » 2021-07-26, 09:58

EDIT: Don't do this! there are better ways.

Code: Select all

var observice=Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
var anti_multi_observer_topic="{insert unique observer topic string}";
if(!observice.enumerateObservers(anti_multi_observer_topic).hasMoreElements()){
  observice.addObserver({},anti_multi_observer_topic,false);//this observer does nothing, it exists for if check above next time script runs
  //insert code to run only first time
}
Replace {insert unique observer topic string} with unique observer topic string.
Replace //insert code to run only first time with code to run only first time.

Use when you need to run something once in script that might run multiple times.
Example: <script> in chrome://browser/content/browser.xul overlay will run every time new browser window is opened.
Example: you need to add only one observer because observers are global for all browser windows in same process.

Links:

User avatar
moonbat
Knows the dark side
Knows the dark side
Posts: 4980
Joined: 2015-12-09, 15:45
Contact:

Re: prevent extension script from running multiple times using XPCOM observers

Unread post by moonbat » 2021-07-26, 10:10

If you don't want something to run multiple times, you put it in a Javascript module that gets loaded only once at browser startup. There's no need for complicated hacks like this.
"One hosts to look them up, one DNS to find them and in the darkness BIND them."

Image
Linux Mint 21 Xfce x64 on HP i5-5200 laptop, 12 GB RAM.
AutoPageColor|PermissionsPlus|PMPlayer|Pure URL|RecordRewind|TextFX

New Tobin Paradigm

Re: prevent extension script from running multiple times using XPCOM observers

Unread post by New Tobin Paradigm » 2021-07-26, 12:05

You mean an xpcom js component.

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 35602
Joined: 2011-08-28, 17:27
Location: Motala, SE
Contact:

Re: prevent extension script from running multiple times using XPCOM observers

Unread post by Moonchild » 2021-07-26, 12:29

It's the wrong approach anyway. Observers are very heavy and will affect performance. You don't need them if you simply use a prototyped object for your code that holds a variable (e.g. _inited) and set/check that. Leverage the native JS paradigms.
"Sometimes, the best way to get what you want is to be a good person." -- Louis Rossmann
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

New Tobin Paradigm

Re: prevent extension script from running multiple times using XPCOM observers

Unread post by New Tobin Paradigm » 2021-07-26, 13:52

Since this "Tutorial" is in question, may I suggest it be moved to UXP Development or the Add-ons Board. In the future it MAY be wise to check with us before posting in here because of issues like this where the "Tutorial" is not advised or misguided.

To the topic, this is the basic way to build an XPCOM JS Component properly. What is suggested by the OP is frankly insane and should not be used.

http://udn.realityripple.com/docs/Mozilla/Tech/XPCOM/Guide/Building_components_in_JavaScript

Additionally, here is are a few functional example which I been using here and there based off older Adblock Plus code:

https://git.binaryoutcast.com/adblocker/src/branch/TRUNK/components/Initializer.js
https://git.binaryoutcast.com/inspector/src/branch/TRUNK/components/initializer.js
https://git.binaryoutcast.com/elemhidehelper/src/branch/TRUNK/components/initializer.js

BZZZZ

Re: prevent extension script from running multiple times using XPCOM observers

Unread post by BZZZZ » 2021-07-28, 06:19

moonbat wrote:
2021-07-26, 10:10
If you don't want something to run multiple times, you put it in a Javascript module that gets loaded only once at browser startup. There's no need for complicated hacks like this.
Thank you! :thumbup:

Script imported by Components.utils.import() run only when imported first time!

Can someone append "THIS IS BAD WAY OF DOING THIS!" to original post?

User avatar
moonbat
Knows the dark side
Knows the dark side
Posts: 4980
Joined: 2015-12-09, 15:45
Contact:

Re: prevent extension script from running multiple times using XPCOM observers

Unread post by moonbat » 2021-07-29, 07:04

I would recommend going through the XUL Tutorial, because even if you're familiar with Javascript, you still need to understand script interaction and how the browser uses them. This isn't a regular webpage script situation after all.
"One hosts to look them up, one DNS to find them and in the darkness BIND them."

Image
Linux Mint 21 Xfce x64 on HP i5-5200 laptop, 12 GB RAM.
AutoPageColor|PermissionsPlus|PMPlayer|Pure URL|RecordRewind|TextFX

Locked