Creating a lock system by utilizing GM_getValue and GM_setValue functions

I have a script in Greasemonkey that checks for updates and prompts the user to download if necessary. The issue arises when multiple tabs are opened simultaneously, causing the script to notify the user in each tab at the same time, which can be frustrating.

The only way I can communicate between instances of the script is through GM_setValue/GM_getValue, giving access to a key/value store.

I am looking to implement a locking system (let's call it GM_setLock/GM_releaseLock) to manage this situation:

GM_setLock();
const tried_update = GM_getValue(available_version);
GM_setValue(available_version, true);
GM_releaseLock();

if (!tried_update) { prompt_user() }

Without locking, multiple instances could read the value before any of them actually set it, resulting in the user being notified more than once.

However, figuring out how to implement locking with just atomic read and write operations - without return previous value capability - is challenging. Any suggestions?

Answer №1

If you're working in Greasemonkey, the syntax might not allow for direct implementation of what you need. However, a similar approach can achieve your desired outcome:

Start by wrapping the upgrade check or relevant function like this:

function RunUpgradeCheck ()
{
    //--- Insert your code here.

    alert ("Upgrade check complete!");
}

.
Next, define RunOnceAcrossTabs() as shown below:

function RunOnceAcrossTabs (lockName, func)
{
    var storedValue = GM_getValue (lockName);
    if (storedValue)
    {
        //--- Add optional timestamp check to clear any old locks.
        return;
    }

    GM_setValue (lockName, new Date().toString());

    //--- Execute the specified function.
    (func)();

    //--- Clear the lock.
    GM_deleteValue (lockName);
}

.
Finally, call the function like this:

RunOnceAcrossTabs("UpgradeLock", RunUpgradeCheck);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues with react-bootstrap component Switches functionality not operating as expected

It looks like the bootstrap switches are not functioning properly. Even the documentation version is not working as expected <Form> <Form.Check type="switch" id="custom-switch" label="Check this switch" /> <Form.Check ...

Three.JS: Utilizing a wireframe material for spheres with the EdgesHelper control

I'm exploring Three.JS for the first time and I have some doubts about whether it's the best tool for my current project. My goal is to create a wireframe material on a simple spherical geometry that looks like this: https://i.sstatic.net/WPfAh ...

Dynamic route fails to return value for ID search

Currently, I am testing by creating an array of users containing their respective IDs and names. There is also a button that triggers an onclick function to add the element's ID to the page's URL in order to establish a dynamic route. However, wh ...

Creating a unique Angular filter involves combining different techniques and functionalities to tailor

Hey there! I'm just diving into the world of Angular JS and I'm looking to filter any Twitter text that comes back containing a hashtag, and turn that word into a clickable link. For example: If the returned twitter text is "The quick brown #f ...

What are the possible reasons for my load function failing intermittently?

I have encountered an issue with my app where sometimes the content is not loaded into a dialog. Most of the time it works perfectly, but occasionally it fails to display the content. Here is the code snippet that I am using: $('#popup_background&apo ...

Understanding the behavior of Bootstrap input-groups when containing hidden sub elements

In a scenario where a button and an input field are enclosed within a div with a bootstrap class named input-group, they expand to occupy the entire width of the enclosing element. However, if the button is hidden, the input field unexpectedly contracts to ...

Uncover the hidden treasure within Vue.js

As a newcomer to Vue, I am still in the process of learning more about it. One challenge I am facing is trying to access and format the value of an item where decimal places and commas are involved. for (let j in data.table.items) { console.log(data.ta ...

Display toggle malfunctioning when switching tabs

I am currently working on implementing a search function with tabbed features. Everything seems to be displaying correctly without any errors. However, the issue arises when I try to click on any tab in order to show or hide specific content from other tab ...

Leveraging Angular js for performing operations in Putty

At the moment, we depend on Putty for connecting to the app server and checking logs. I am looking for a solution that would allow me to automate this process using angular js. Is it possible to send commands from my angular js or any other client-side a ...

Error occurred while attempting to parse JSON on comment system

I've run into some issues with my comments system. Everything was working perfectly yesterday, but today I started getting errors. I'm using json to post comments without reloading the page, but now I'm encountering a "SyntaxError: JSON.pars ...

Managing dynamically loaded scripts in Meteor.js

This poses an interesting query: is there a way to regulate which scripts a client is provided with? I've partitioned my code into dynamically loadable segments using the import('dynamically_loadable_file') method, but each time it's ca ...

Design a customizable input field to collect HTML/JS tags

I'm looking to incorporate a similar design element on my website. Can anyone offer me some guidance? Check out the image here ...

Retrieve the overall number of Formik errors for a specific field array

In the given Yup validation setup below, there is a nested Formik FieldArray: parentLevel: Yup.array().of( Yup.object({ childrenLevel: Yup.array().of( Yup.object({ childName: Yup.string().required('Name is required') ...

Manipulate HTML Table to Obtain Value of First Column Cell When Another Cell is Clicked using Javascript

I have a PHP web application where I am retrieving data from MySql and displaying it in the HTML table below. <table id = "table" width="500px" cellpadding=2 celspacing=5 border=2 > <tr> <th>Subject Code</th> <th>Subje ...

Revamping tree organization with Accelerando

I recently made the switch from XML to JSON data structures in a small web project. As I was looking for ways to transform the data, similar to how XSLT is used with XML, I came across an interesting library called . However, I encountered a challenge whe ...

The .value property on the form group displays numeric values as either null or an empty string

I'm encountering an issue with extracting data from a form group. Within my code, there is a formGroup named lineitemForm, and I am attempting to structure this form group as follows: private formatTransferData() { const depositDates = this.get ...

Executing code within Vue js $set method's callback

For my v-for loop that generates a list of flights, I have implemented functionality where the user can click on a "flight details" button to send an AJAX request to the server and append new information to the results array. To update the view accordingly ...

Discover the highest value within a JSON object and save it as a variable

I am working on a d3 graph with a specified Y-axis range in my code: var yScale = d3.scaleLinear() .domain([0, 1]) // input .range([height, 0]); // output However, I have realized that a scale of 0 to 1 may not be the most suitable for my data. ...

What are the methods used by Typescript 2 to ensure non-nullable types?

While it is commonly understood that the compiler performs static type checking, I am curious about the specific methods it employs to ensure that nullable types are not inadvertently used. ...

Is it possible to implement CSS code from a server request into a React application?

With a single React app that hosts numerous customer websites which can be customized in various ways, I want to enable users to apply their own CSS code to their respective sites. Since users typically don't switch directly between websites, applying ...