Is there a method to generate TouchEvents or a similar functionality in Firefox?

I am currently developing a browser extension that is able to generate simulated TouchEvents. My project is functional in Chrome, but I have encountered an issue with Firefox not supporting the TouchEvents feature. When attempting to create a new TouchEvent, Firefox displays the following error message:

ReferenceError: TouchEvent is not defined

I am exploring options to either implement TouchEvent within the extension or find an alternative method to dispatch an event with all the necessary properties similar to a TouchEvent.

Creating a CustomEvent and adding additional properties such as touches, targetTouches, and changedTouches to mimic a TouchEvent is proving to be ineffective, as these properties are not maintained once the event reaches the webpage.

Does anyone know of a solution to achieve my goal, or should I abandon hope of making this work on Firefox?

Answer №1

After struggling to make the accepted solution work due to difficulties in constructing the necessary touches for the initDict, I finally found a workaround that suited my needs:

function dispatchTouchEvent(eventName, details) {
    const uiEvent = document.createEvent('UIEvent');
    uiEvent.initUIEvent(eventName, true, true, window, 1);
    const event = Object.assign(uiEvent, {touches: [details]});
    details.target.dispatchEvent(event);
}

Furthermore, here's an example of how to trigger it with a touchstart event on the body element at coordinates (50, 100):

dispatchTouchEvent('touchstart', {identifier: 0, target: document.body, pageX: 50, pageY: 100});

In my use case, only setting the pageX and pageY properties was necessary. However, depending on your specific requirements, you may need to also define clientX, clientY, screenX, or screenY.

Answer №2

For those facing a similar question, here is the solution I came across. You can develop your own custom class that extends UIEvent in the following manner:

class TouchEvent extends UIEvent {
    constructor(name, initDict) {
        super(name, initDict);
        this.touches = initDict.touches;
        this.targetTouches = initDict.targetTouches;
        this.changedTouches = initDict.changedTouches;
    }
}

It's worth noting that these events need to be created by a script running within the same domain as the handler that will handle them; otherwise, you may encounter a

Permission denied to access property
error. To address this issue, I utilized my extension's content script to inject a script element into the current document's body and assigned its textContent property to my JavaScript code responsible for generating and dispatching the touch events.

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

Persist state in Vue by using the `vue-persistedstate` reducer

Currently, I am utilizing vue-persistedstate with specific modules that are set to be persisted using the path attribute. This setup is functioning smoothly. However, I encountered an issue when attempting to combine it with the reducer. In this scenario, ...

Tips for incorporating a live URL using Fetch

I'm having some trouble with this code. Taking out the &type = {t} makes it work fine, but adding it causes the fetch to not return any array. let n = 12 let c = 20 let t = 'multiple' let d = 'hard' fetch(`https://opentdb.com/ ...

Modifying button appearance based on state change in AngularJS

I have a button with a grey color and I want to create two different states for it: one that is selected (blue) and another that is in an idle state (grey). Currently, I am working on Angularjs but I am fairly new to this platform. Could you kindly provid ...

Verifying the presence of an object in an array based on its value using TypeScript

Having the following dataset: roles = [ {roleId: "69801", role: "ADMIN"} {roleId: "69806", role: "SUPER_ADMIN"} {roleId: "69805", role: "RB"} {roleId: "69804", role: "PILOTE"} {roleId: "69808", role: "VENDEUR"} {roleId: "69807", role: "SUPER_RB"} ] The o ...

Utilizing Angular Schema Form to Populate Form Fields with JSON Data

As a hardware engineer venturing into the realm of creating an in-house software tool, I initially thought it would be a straightforward process. However, I've encountered a plethora of unknowns hindering my progress. My goal is to develop an interna ...

Converting a tree structure into JSON with the help of tree-model-js

Would it be possible to find a method to convert a TreeModel into a JSON string for storage purposes and then use tree.parse() to reconstruct it later on? Whenever trying JSON.stringify(root), an error is thrown due to cyclic references caused by children ...

Retrieve the chosen language while utilizing the $translate feature

I have successfully integrated the $translate project from this source into my AngularJS application. While I have configured the default language in my app.config() using $translateProvider, I am now wondering how to retrieve the selected language within ...

Jquery Triggers Failing to Work Following Ajax Request

I have worked on 2 PHP pages, called "booking.php" and "fetch_book_time.php". Within my booking.php (where the jquery trigger is) <?php include ("conn.php"); include ("functions.php"); ?> $(document).ready(function(){ $(".form-group"). ...

What is the best way to extract a specific section of JSON data using nodejs?

I am currently working on a nodeJS script that listens for webhook events and then adds the data to firebase. The script is functioning well with the following code: let data = { addresses: event.data["addresses"], c ...

Having trouble muting the audio on my Vue audio player

I'm facing some challenges with muting the audio within my vue app. I have a list of songs that can be played, paused, shuffled, etc., but I can't seem to get the mute function working. Here's what I have in the JavaScript: mute() ...

Learn the process of encoding a string in JavaScript or jQuery and then decoding it with PHP

I am facing an issue with updating a field in the database using Ajax and PHP. Everything runs smoothly except when there are special characters present, like this: اللهم اني اشكو اليك ضعف قوتي وقلة حيلتي وهواني عل ...

Learn how to stop the automatic scroll-to-top behavior on specific links within your React-Router application by implementing a custom ScrollToTop component

While utilizing React-Router and a custom ScrollToTop component to reset the scroll position on navigation, I encountered a specific scenario where I required the scroll position to remain unchanged when clicking on a category item. Custom ScrollToTop Com ...

Extracting information from a child select element within a parent component with ReactJS

My child component includes a select form element that queries my API to populate a select box with the data. I am trying to pass the selected option back to the parent component using an OnChange function, so that I can send the data back to the server. H ...

Ajax encountered an error while attempting to load the requested resource

jQuery('input').on('click',function(e){ $.getJSON( "/data.json", function(info){ var name = data.name; } ); }); Every time we click, a JSON request is supposed to be made. But unfortunately ...

How can you choose multiple options in a Select2 dropdown when it first loads?

I'm having trouble correctly loading a JSON array of selected items into an existing select2 dropdown. I want certain items to be pre-selected when the page loads, but I'm struggling with the syntax. Consider the following JSON array, stored in ...

How can you achieve a seamless or infinite scrolling effect on a webpage?

My idea is as follows: Imagine a long web page where, instead of the scrolling coming to an abrupt stop when the user reaches the end, I want the page to reload from the bottom and allow the scrolling to continue seamlessly. Specifics Picture this - as ...

Effortless AJAX Submission and MySQL Data Refresh

I've hit a roadblock and can't seem to figure things out. I'm getting rid of my current code because it's not working well across different browsers, particularly when it comes to ajax submission. Could someone please provide me with a ...

Error encountered in 11ty: Invalid operation - eleventyConfig.addPassthroughCopy is not a recognized function

Attempting to integrate my CSS and JavaScript codes into 11ty has been a bit challenging for me. I recently discovered that 11ty utilizes something called .11ty.js, so I decided to utilize the addPassthroughCopy() function. Below is the script I tried: mo ...

Updating switch data on click using Vuejs: A step-by-step guide

Could someone please explain to me how to swap two different sets of data when clicked? For instance, let's say we have the following data: data() { return { data_one: [ { name: "Simo", ...

What is the best method for transferring the value of a useState variable between separate components in React?

I am working on two components, editor.js and toolbar.js. My goal is to pass the activeTool value from Toolbar.js to editor.js so it can be updated after each click. Editor.js import Toolbar from './Toolbar.js' export default function Editor() ...