#I was trying to add :emoji: feature on instagram.com but....

76 messages · Page 1 of 1 (latest)

grave thistle
#

my code:-

// ==UserScript==
// @name         Instagram Direct :sob: to 😭
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Replaces :sob: with 😭 in Instagram Direct message textbox
// @author       Your Name
// @match        https://www.instagram.com/direct/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Watch for keypress events on the document object
    document.addEventListener('keydown', function(event) {
        // Check if the keypress was triggered in an input element
        if (event.target.tagName === 'INPUT' || event.target.tagName === 'TEXTAREA') {
            const messageInput = event.target;

            // Replace :sob: with 😭
            messageInput.value = messageInput.value.replace(/:sob:/g, '😭');
        }
    });
})();

when i type :sob: it doesnt do anything but when i type :sob:/" it gives 😭/
can someone pointout the error

obsidian escarp
#

So when you add the /" to the text box the code detects the :sob: bit?

junior rampart
#

Errata:
There was nothing wrong with your code.

#

The "problem" is JS is waiting for a new input to update the value. If you type :sob: and then press any key, like shift for example, it will then update.

#

Here's my try:

const input = document.getElementById("field")

input.addEventListener("focus", ()=> {
    document.addEventListener("keydown", e => {
        const message_input = e.target
        message_input.value = message_input.value.replace(/:sob:/g, 'G');
    })
})
#

you don't need all those conditions btw, just check if the input is on focus with the "focus" listener :)

#

But, if you still want a fix for your condition, you can use an update function to check it every frame!


const input = document.getElementById("field")

// Check if the "input field" is on "focus
input.addEventListener("focus", ()=> {

    //Checks if some key is being pressed
    input.addEventListener("keydown", e => {
        const message_input = e.target

        //Defines an update function, it will run until the // element loses it focus
        const update = ()=> {
            //updates the value every frame
            message_input.value = message_input.value.replace(/:sob:/g, '😭');

            //calls itself
            requestAnimationFrame(update)
        }
        //calls the update function to finally start the // checking
        update()
    })
})

#

hope it helps you :)

#

I find really difficult to see with discord's text formatting, so here's an image:

grave thistle
#

sorry for checking this late

#

i wa sbust with school

grave thistle
grave thistle
grave thistle
#

focus listener also doesnt find

#

the right input

#
            const messageValue = messageInput.value.replace(/:sob:/g, ':sob:');
            messageInput.value = messageValue;```
#

thhis does replace the text all fine

#

but

#

there are two tags that keep theh text value

#

the vaalue in this tag isnt changing

grave thistle
#
// @name         Instagram Direct :sob: to 😭
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  Replaces :sob: with 😭 in Instagram Direct message textbox and adds a space on a new line
// @author       Your Name
// @match        https://www.instagram.com/direct/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Watch for input events on the document object and the targeted textarea
    const targetTextarea = document.evaluate('/html/body/div[2]/div/div/div/div[1]/div/div/div/div[1]/div[1]/div/div[2]/div/section/div/div/div/div/div[2]/div[2]/div/div[2]/div/div/div[2]/textarea', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

    document.addEventListener('input', function(event) {
        // Check if the input event was triggered in an input element or any textarea
        if (event.target.tagName === 'INPUT' || event.target.tagName === 'TEXTAREA') {
            const messageInput = event.target;

            // Replace :sob: with 😭 in the value of the message input element
            const messageValue = messageInput.value.replace(/:sob:/g, '😭');
            messageInput.value = messageValue;

            // Dispatch a new input event to ensure any other event listeners are also notified
            messageInput.dispatchEvent(new Event('input', { bubbles: true }));
        }

        // Check if the input event was triggered in the targeted textarea
        if (event.target === targetTextarea) {
            const messageInput = event.target;

            // Replace :sob: with 😭 in the value of the targeted textarea
            const messageValue = messageInput.value.replace(/:sob:/g, '😭');
            messageInput.value = messageValue;

            // Dispatch a new input event to ensure any other event listeners are also notified
            messageInput.dispatchEvent(new Event('input', { bubbles: true }));
        }
    });
})();


#

this should workkk but it is not

junior rampart
#

Okay so just to settle it down, you are injecting this JS on Instagram page right?

junior rampart
#

Let me try it then

grave thistle
#

okay! 👍

junior rampart
#

Oh, friendly advice:
Be cautious with js injections on Instagram.
Got banned many times, and also they are constantly changing the html, is not that reliable.

grave thistle
junior rampart
#
const input = document.querySelector("textarea")

input.addEventListener("focus", ()=> {

    input.addEventListener("keydown", e => {
        const message_input = e.target

        const update = ()=> {
            message_input.value = message_input.value.replace(/:sob:/g, '😭');
            requestAnimationFrame(update)
        }
        update()
    })
})
#

inject this, and give it a try

#

It's banneble when you start to messing with requests. For example:
Do you know the "followers" box? That you need to keep scrolling to load in desktop?

#

Every scroll is a req, so its bannable

grave thistle
#

so unfollow bot should also be

junior rampart
#

probably yes

grave thistle
#

nope nothing

junior rampart
#

what browser are you using?

grave thistle
#

chrome latest Version 110.0.5481.104 (Official Build) (64-bit)

junior rampart
#

let try it

#

wait just to clarify, because I just did it.
I've copied the text from discord and it came with the " ``` "

#

did you cleaned those?

grave thistle
#

i tried a lot of other methods

#

but idk why

#

i cant change that particular tag

junior rampart
#

wait, which tag?

#

Maybe I've understood it wrong. You want to change the given text to the emoji, or change some inner HTML tag?

grave thistle
#

one is our input

formal gullBOT
#

@junior rampart

File Attachments Not Allowed

For safety reasons we do not allow file and video attachments.

Code Formatting

You can share your code using triple backticks like this:
```
YOUR CODE
```

Large Portions of Code

For longer scripts use Hastebin or GitHub Gists and share the link here

Ignored these files
  • 2023-02-21_12-03-03_-_Trim.mkv
junior rampart
grave thistle
#

whhch is getting replaced succesfully

#

in the code i sent you

#

and the other one iss

#

this one

#

this tag only updates if i manually kkeypress

#

so i noticed

#

my code replaces the emoji in input

#

but not in this textarea

#

final message gets send thru this only

junior rampart
#

got it.
Just test it, it can be done, but it's tricky.

#

let me try

grave thistle
#

okkay!

#

i been trying since two days lol i cant find an optimal solution to edit the value in this tag

junior rampart
#

Okay, here's the thing:
try the code I've sent, buuuut, instead of just pressing enter, try to press the space bar before

junior rampart
#

so, apparently the user needs to update the input after the change, so Instagram accepts it as a valid emoji

grave thistle
junior rampart
#

actually, changing the html value vs. the user doing an keyboard action are different things.
For example, you can't play audio on a page you've never interacted with

#

usually for security reasons

#

sure, the text field is being updated, but only in your screen. Instagram is calling something somewhere to catch your input everytime you type