Intro.js Highlight an Element in an Iframe

I’m attempting to use the Intro.js library to create tooltips for a number of elements on a web page. The page includes an iframe, and I want to add a tooltip on a button that is rendered on the page inside that iframe.

Unfortunately, the tooltip will not focus on the button inside the iframe. The tooltip is shown in the upper left corner of the screen. I can place a tooltip on the iframe itself but not on any element within that iframe.

I’ve attempted to set the target of the tooltip by getting the iframe document and using the querySelector function to highlight the button in the iframe.

element: document.querySelector('#iframeContent').contentDocument.querySelector('#startButton')

Does anyone know if its possible to have the Intro.js library focus its tooltip on an element in an iframe?

Please find the complete example on Stackblitz here.

I need help from postman experts

(

   "Credit":    200,

"ID":   "A2"

)

Is there a way to send more than one order in ascending order +200 in each operation with one click of a button? I will explain what is required: I am trying to raise the “Credit” to level (300000) but it requires me to start from number 200 and each time I add another 200 as an example: 200, 400, 600, 800…

(

   "Credit":    200,

"ID":   "A2"

)


(

   "Credit":    400,

"ID":   "A2"

)


(

   "Credit":    600,

"ID":   "A2"

)

If we divide 300,000 by 200 this requires me to press 1500 POST in postman to reach number 300000 In addition to modifying and increasing the value “Credit”. This is very tiring.

I need something that does this automatically.

I am trying to find an automatic solution to save time.

How to use JavaScript or jQuery to select an anchor tag using the enter key

I’m stuck trying to select an anchor tag inside of an unordered list using the keyboard arrow keys and hitting the enter key. The arrow keys will highlight the link, triggering the native browser behavior text-decoration:underline, but using the following produces nothing.

$('body').on('keydown', function(e) {
   var code = (e.keyCode ? e.keyCode : e.which);
   if(code == 13) {
      console.log($('body').find('a:active, a:focus'));
   }
})

Fabric.js how to subclass the `fabric.Object`

I’m working on a project where fabric.js (version 5.3) is used in several different places. I have recently built a requested feature with it and in the meantime, I had to extend some fabric classes like fabric.Object, fabric.Shadow, fabric.Line etc. and their prototype methods to apply custom logic.

I recently figured that these changes were affecting every code where fabric is imported and used and due to this fact, it makes some of these codes to not work properly. I want to convert my extend code to subclasses so I can avoid this issue.

Below is the code I use to extends fabric’s classes and their methods:

// Ensure the fillEnabled property is included when serializing or cloning Fabric.js objects.
fabric.Object.prototype.initialize = (function (originalInitialize) {
  return function (options) {
    originalInitialize.call(this, options);
    this.fillEnabled = options?.fillEnabled ?? true; // Default to true if not provided
    this.prevStrokeWidth = options?.prevStrokeWidth ?? null; // Default to null if not provided
    this.backgroundEnabled = options?.backgroundEnabled ?? true; // Default to true if not provided
  };
})(fabric.Object.prototype.initialize);

// Extend or patch the fabric.Object.prototype._render method to skip shadow rendering if enabled is false.
fabric.Object.prototype.render = (function (originalRender) {
  return function (ctx) {
    let prevBackgroundColor = null;
    let prevShadow = null;
    let prevFill = null;

    if (this.backgroundEnabled === false) {
      // Temporarily disable backgroundColor for this rendering pass
      prevBackgroundColor = this.backgroundColor;
      this.backgroundColor = null;
    }
    if (this.fillEnabled === false) {
      // Temporarily disable fill for this rendering pass
      prevFill = this.fill;
      this.fill = null; // Disable fill
    }
    if (this.shadow && this.shadow.enabled === false) {
      // Temporarily remove the shadow if 'enabled' is false
      prevShadow = this.shadow;
      this.shadow = null;
    }

    // Call the original render logic without the disabled properties such as shadow
    originalRender.call(this, ctx);

    if (prevBackgroundColor !== null) {
      // Restore the backgroundColor property
      this.backgroundColor = prevBackgroundColor;
    }
    if (prevShadow !== null) {
      // Restore the shadow property
      this.shadow = prevShadow;
    }
    if (prevFill) {
      // Restore the original fill
      this.fill = prevFill;
    }
  };
})(fabric.Object.prototype.render);

// Override or extend the fabric.Object methods to check the fillEnabled property before rendering the shadow.
fabric.Object.prototype.toObject = (function (originalToObject) {
  return function () {
    // Exclude objects marked with `excludeFromExport`
    if (this.excludeFromExport === true) {
      return null;
    }

    const propertiesToInclude = [
      'points',
      'hoverCursor',
      'selectable',
      'evented',
      'objectCaching',
      '_controlsVisibility',
    ];

    const obj = originalToObject.call(this, propertiesToInclude);

    obj.id = this.id;
    obj.relatedTo = this.relatedTo;
    obj.name = this.name;
    obj.fillEnabled = this.fillEnabled ?? true;
    obj.prevStrokeWidth = this.prevStrokeWidth ?? null;
    obj.backgroundEnabled = this.backgroundEnabled ?? true;

    if (this.type === 'arrow-customizable') {
      obj.x1 = this.x1;
      obj.y1 = this.y1;
      obj.x2 = this.x2;
      obj.y2 = this.y2;
      obj.arrowFillEnabled = this.arrowFillEnabled || true;
      obj.arrowFlapAngle = this.arrowFlapAngle || 30;
      obj.arrowFlapLength = this.arrowFlapLength || 0.1;
      obj.headCorner = this.headCorner || null;
      obj.tailCorner = this.tailCorner || null;
      obj.points = this.points || [this.x1, this.y1, this.x2, this.y2];
    }

    return obj;
  };
})(fabric.Object.prototype.toObject);

fabric.ArrowCustomizable = fabric.util.createClass(fabric.Line, {
  type: 'arrow-customizable',

  initialize(element, options) {
    options || (options = {});
    this.arrowFillEnabled = options.arrowFillEnabled || true; // Default arrow fill flag value
    this.arrowFlapAngle = options.arrowFlapAngle || 30; // Default flap angle in degrees
    this.arrowFlapLength = options.arrowFlapLength || 0.1; // Default flap length as percentage
    this.callSuper('initialize', element, options);
  },

  _render(ctx) {
    const arrowFlapAngleRad = (this.arrowFlapAngle * Math.PI) / 180; // Convert degrees to radians
    const arrowFlapLength = (this.arrowFlapLength / 2) * this.length(); // Length as a percentage of the arrow body
    const arrowHalfWidth = Math.tan(arrowFlapAngleRad) * arrowFlapLength; // Half-width of arrowhead

    // Calculate rotation angle of the arrow
    const xDiff = this.x2 - this.x1;
    const yDiff = this.y2 - this.y1;
    const angle = Math.atan2(yDiff, xDiff);

    ctx.beginPath();

    const p = this.calcLinePoints();
    ctx.moveTo(p.x1, p.y1);

    if (this.arrowFillEnabled) {
      const arrowFlapLengthDoubled = arrowFlapLength * 2;
      ctx.lineTo(
        p.x2 - arrowFlapLengthDoubled * Math.cos(angle),
        p.y2 - arrowFlapLengthDoubled * Math.sin(angle)
      );
    } else {
      const tipOffset = this.strokeWidth / 2;
      ctx.lineTo(
        p.x2 - tipOffset * Math.cos(angle),
        p.y2 - tipOffset * Math.sin(angle)
      );
    }

    ctx.lineWidth = this.strokeWidth;
    ctx.strokeStyle = this.stroke;
    this._renderStroke(ctx);

    ctx.save();

    // Adjust the line's endpoint to end at the base of the arrowhead
    const lineEndX =
      (this.x2 - this.x1) / 2 - arrowFlapLength * Math.cos(angle);
    const lineEndY =
      (this.y2 - this.y1) / 2 - arrowFlapLength * Math.sin(angle);

    // Translate to arrow tip and rotate to match arrow direction
    ctx.translate(lineEndX, lineEndY);
    ctx.rotate(angle);

    // Draw the arrowhead
    if (this.arrowFillEnabled) {
      ctx.beginPath();
      ctx.moveTo(arrowFlapLength, 0); // Arrow tip
      ctx.lineTo(-arrowFlapLength, arrowHalfWidth); // Bottom edge of arrowhead
      ctx.lineTo(-arrowFlapLength, -arrowHalfWidth); // Top edge of arrowhead
      ctx.closePath();
    } else {
      ctx.moveTo(-arrowFlapLength, arrowHalfWidth); // Bottom edge of arrowhead
      ctx.lineTo(arrowFlapLength, 0); // Arrow tip
      ctx.lineTo(-arrowFlapLength, -arrowHalfWidth); // Top edge of arrowhead
    }

    if (this.arrowFillEnabled) {
      ctx.fillStyle = this.stroke;
      ctx.fill();
    } else {
      ctx.lineWidth = this.strokeWidth;
      ctx.strokeStyle = this.stroke;
      ctx.stroke();
    }
    ctx.restore();
  },

  // Method to calculate the length of the arrow body
  length() {
    const xDiff = this.x2 - this.x1;
    const yDiff = this.y2 - this.y1;
    return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
  },
});

// Ensure the arrow properties are included during serialization/deserialization
fabric.ArrowCustomizable.fromObject = function (object, callback) {
  return fabric.Object._fromObject(
    'ArrowCustomizable',
    {
      ...object,
      points: [object.x1, object.y1, object.x2, object.y2],
      arrowFlapAngle: object.arrowFlapAngle,
      arrowFlapLength: object.arrowFlapLength,
    },
    callback,
    'points'
  );
};

const canvas = new fabric.Canvas('canvas');

let arrow = new fabric.ArrowCustomizable([25, 25, 300, 200], {
  strokeWidth: 1,
  stroke: '#000000',
});
canvas.add(arrow);
#canvas {
  border: 1px solid black;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/fabric.min.js" type="text/javascript"></script>
</head>
<body>
  <canvas width="500" height="300" id="canvas"></canvas>
</body>
</html>

As you can see, in my code I extend the fabric.Line class to subclass it and create fabric.ArrowCustomizable class. I want to be able to create a custom general subclass of fabric.Object class which we can call fabric.CustomObject to apply my custom render mechanism to all objects extending it. For the fabric.ArrowCustomizable class, I will have to subclass my fabric.CustomObject to subclass fabric.CustomLine which I will use to subclass fabric.ArrowCustomizable class. Below was my first attempt on coding this subclassing but as you can see I can not see my arrow being drawn/rendered properly.

fabric.CustomObject = fabric.util.createClass(fabric.Object, {
  initialize: function (options) {
    options || (options = {});
    this.callSuper('initialize', options);
    this.fillEnabled = options.fillEnabled ?? true;
    this.prevStrokeWidth = options.prevStrokeWidth ?? null;
    this.backgroundEnabled = options.backgroundEnabled ?? true;
  },

  render: function (ctx) {
    let prevBackgroundColor = null;
    let prevShadow = null;
    let prevFill = null;

    if (this.backgroundEnabled === false) {
      // Temporarily disable backgroundColor for this rendering pass
      prevBackgroundColor = this.backgroundColor;
      this.backgroundColor = null;
    }
    if (this.fillEnabled === false) {
      // Temporarily disable fill for this rendering pass
      prevFill = this.fill;
      this.fill = null; // Disable fill
    }
    if (this.shadow && this.shadow.enabled === false) {
      // Temporarily remove the shadow if 'enabled' is false
      prevShadow = this.shadow;
      this.shadow = null;
    }

    // Call the original render logic without the disabled properties such as shadow
    this.callSuper('render', ctx);

    if (prevBackgroundColor !== null) {
      // Restore the backgroundColor property
      this.backgroundColor = prevBackgroundColor;
    }
    if (prevShadow !== null) {
      // Restore the shadow property
      this.shadow = prevShadow;
    }
    if (prevFill) {
      // Restore the original fill
      this.fill = prevFill;
    }
  },

  toObject: function () {
    // Exclude objects marked with `excludeFromExport`
    if (this.excludeFromExport === true) {
      return null;
    }

    const propertiesToInclude = [
      'points',
      'hoverCursor',
      'selectable',
      'evented',
      'objectCaching',
      '_controlsVisibility',
    ];

    const obj = this.callSuper('toObject', propertiesToInclude);

    obj.id = this.id;
    obj.name = this.name;
    obj.fillEnabled = this.fillEnabled ?? true;
    obj.prevStrokeWidth = this.prevStrokeWidth ?? null;
    obj.backgroundEnabled = this.backgroundEnabled ?? true;

    if (this.type === 'arrow-customizable') {
      obj.x1 = this.x1;
      obj.y1 = this.y1;
      obj.x2 = this.x2;
      obj.y2 = this.y2;
      obj.arrowFillEnabled = this.arrowFillEnabled || true;
      obj.arrowFlapAngle = this.arrowFlapAngle || 30;
      obj.arrowFlapLength = this.arrowFlapLength || 0.1;
      obj.headCorner = this.headCorner || null;
      obj.tailCorner = this.tailCorner || null;
      obj.points = this.points || [this.x1, this.y1, this.x2, this.y2];
    }

    return obj;
  },
});

fabric.CustomLine = fabric.util.createClass(fabric.CustomObject, {
  type: 'custom-line',

  initialize: function (points, options) {
    options || (options = {});

    options.points = points ?? [0, 0, 0, 0];
    options.x1 = points[0];
    options.y1 = points[1];
    options.x2 = points[2];
    options.y2 = points[3];

    this.callSuper('initialize', options);
  },

  calcLinePoints: fabric.Line.prototype.calcLinePoints,
});

fabric.ArrowCustomizable = fabric.util.createClass(fabric.CustomLine, {
  type: 'arrow-customizable',

  initialize(points, options) {
    options || (options = {});

    this.arrowFillEnabled = options.arrowFillEnabled || true;
    this.arrowFlapAngle = options.arrowFlapAngle || 30;
    this.arrowFlapLength = options.arrowFlapLength || 0.1;

    this.callSuper('initialize', points, options);
  },

  _render(ctx) {
    const arrowFlapAngleRad = (this.arrowFlapAngle * Math.PI) / 180;
    const arrowFlapLength = (this.arrowFlapLength / 2) * this.length();
    const arrowHalfWidth = Math.tan(arrowFlapAngleRad) * arrowFlapLength;

    // Calculate rotation angle of the arrow
    const xDiff = this.x2 - this.x1;
    const yDiff = this.y2 - this.y1;
    const angle = Math.atan2(yDiff, xDiff);

    ctx.beginPath();

    const p = this.calcLinePoints();
    ctx.moveTo(p.x1, p.y1);

    if (this.arrowFillEnabled) {
      const arrowFlapLengthDoubled = arrowFlapLength * 2;
      ctx.lineTo(
        p.x2 - arrowFlapLengthDoubled * Math.cos(angle),
        p.y2 - arrowFlapLengthDoubled * Math.sin(angle)
      );
    } else {
      const tipOffset = this.strokeWidth / 2;
      ctx.lineTo(
        p.x2 - tipOffset * Math.cos(angle),
        p.y2 - tipOffset * Math.sin(angle)
      );
    }

    ctx.lineWidth = this.strokeWidth;
    ctx.strokeStyle = this.stroke;
    this._renderStroke(ctx);

    ctx.save();

    // Adjust the line's endpoint to end at the base of the arrowhead
    const lineEndX =
      (this.x2 - this.x1) / 2 - arrowFlapLength * Math.cos(angle);
    const lineEndY =
      (this.y2 - this.y1) / 2 - arrowFlapLength * Math.sin(angle);

    // Translate to arrow tip and rotate to match arrow direction
    ctx.translate(lineEndX, lineEndY);
    ctx.rotate(angle);

    // Draw the arrowhead
    if (this.arrowFillEnabled) {
      ctx.beginPath();
      ctx.moveTo(arrowFlapLength, 0); // Arrow tip
      ctx.lineTo(-arrowFlapLength, arrowHalfWidth); // Bottom edge of arrowhead
      ctx.lineTo(-arrowFlapLength, -arrowHalfWidth); // Top edge of arrowhead
      ctx.closePath();
    } else {
      ctx.moveTo(-arrowFlapLength, arrowHalfWidth); // Bottom edge of arrowhead
      ctx.lineTo(arrowFlapLength, 0); // Arrow tip
      ctx.lineTo(-arrowFlapLength, -arrowHalfWidth); // Top edge of arrowhead
    }

    if (this.arrowFillEnabled) {
      ctx.fillStyle = this.stroke;
      ctx.fill();
    } else {
      ctx.lineWidth = this.strokeWidth;
      ctx.strokeStyle = this.stroke;
      ctx.stroke();
    }
    ctx.restore();
  },

  calcLinePoints: fabric.Line.prototype.calcLinePoints,

  // Method to calculate the length of the arrow body
  length() {
    const xDiff = this.x2 - this.x1;
    const yDiff = this.y2 - this.y1;
    return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
  },
});

// Ensure the arrow properties are included during serialization/deserialization
fabric.ArrowCustomizable.fromObject = function (object, callback) {
  return fabric.Object._fromObject(
    'ArrowCustomizable',
    {
      ...object,
      points: [object.x1, object.y1, object.x2, object.y2],
      arrowFlapAngle: object.arrowFlapAngle,
      arrowFlapLength: object.arrowFlapLength,
    },
    callback,
    'points'
  );
};

const canvas = new fabric.Canvas('canvas');

let arrow = new fabric.ArrowCustomizable([25, 25, 300, 200], {
  strokeWidth: 1,
  stroke: '#000000'
});
canvas.add(arrow);
#canvas {
  border: 1px solid black;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/fabric.min.js" type="text/javascript"></script>
</head>
<body>
  <canvas width="500" height="300" id="canvas"></canvas>
</body>
</html>

How to solve CORS error for proxy server?

I’m trying to solve CORS error with a proxy server.
The proxy uses this code:

  target: 'https://someUrl', 
  changeOrigin: true, 
});
app.use('/api', exampleProxy);
app.listen(3001, () => {
  console.log(`Proxy server is running at http://localhost:${3001}`);
}); 

The client is sending a request to:
function doRequest() {
const urlMexcPairs = http://localhost:3001/api;}

But I still getting CORS error.
Cant use 3000 port for both server and client as getting an error once starting them up :
“Something is already running on port 3000.
Would you like to run the app on another port instead?”

What is the best solution?

How can I play an animation after the previous one is finished?

I have been trying the typewriter animation, and it went well!

.typewriter{
  display: flex;
  justify-content: center;
}

.typewriter .p1,.p2{
  font-family: monospace;
  font-size: 1.5rem;
/*   margin-inline: auto; */
  overflow: hidden;
/* keeps on a single line */
  white-space: nowrap;
/* cursor */
  border-right: 3px solid;
/* steps = number of characters   */
  animation: typing 3s steps(22) forwards, blink 1s step-end infinite;
}

.typewriter .p2{
  font-size: 3rem;
  animation-delay: after previos;
}

@keyframes typing{
  from{
    width: 0;
  }
  
  to{
    width: 100%;
  }
}

@keyframes blink{
  50%{
    border-color: transparent;
  }
}
<div class = "typewriter">
  <div>
    <p class = "p1">Hello, Welcome to CodePen!</p>
    <p class = "p2">Let's get started!</p>
  </div>
</div>

But I am confused, about how to play the second line animation after the first one.

I have tried animation-delay but it doesn’t work as I thought.

This could be made possible using javascript as well but I wanted to know whether there are any direct methods using CSS attributes.

AioRTC one-way video call error in python

when i try to run this code:

server.py:

    import asyncio
import cv2
from aiortc import RTCPeerConnection, RTCSessionDescription, VideoStreamTrack
from aiohttp import web
from pyngrok import ngrok
class OpenCVVideoStreamTrack(VideoStreamTrack):
    def __init__(self):
        super().__init__()
        self.cap = cv2.VideoCapture(0)

        if not self.cap.isOpened():
            raise RuntimeError("Nu s-a putut deschide camera video. Verifică conexiunea la cameră.")

    async def recv(self):
        frame = await asyncio.get_event_loop().run_in_executor(None, self._get_frame)
        return frame

    def _get_frame(self):
        ret, frame = self.cap.read()
        if not ret:
            raise RuntimeError("Eroare la capturarea cadrelor video.")
        frame = cv2.resize(frame, (640, 480))
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        return frame

pcs = set()
async def offer(request):
    params = await request.json()
    pc = RTCPeerConnection()
    pcs.add(pc)
    video_track = None
    if cv2.VideoCapture(0).isOpened():
        video_track = OpenCVVideoStreamTrack()
        video_track.direction = "sendrecv"
        pc.addTrack(video_track)
    else:
        return web.Response(status=500, text="Eroare la inițializarea camerei. Camera nu este disponibilă.")

    @pc.on("iceconnectionstatechange")
    async def on_iceconnectionstatechange():
        print(f"ICE connection state: {pc.iceConnectionState}")
        if pc.iceConnectionState == "failed":
            await pc.close()
            pcs.discard(pc)

    if "sdp" not in params or "type" not in params:
        return web.Response(status=400, text="Oferta este invalidă.")

    offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"])
    await pc.setRemoteDescription(offer)
    answer = await pc.createAnswer()
    await pc.setLocalDescription(answer)
    return web.json_response(
        {"sdp": pc.localDescription.sdp, "type": pc.localDescription.type}
    )

async def cleanup(app):
    for pc in pcs:
        await pc.close()
    pcs.clear()

app = web.Application()
app.on_shutdown.append(cleanup)
app.router.add_post("/offer", offer)
public_url = ngrok.connect(8080)
print(f"Ngrok public URL: {public_url}")
web.run_app(app, host="0.0.0.0", port=8080)

web.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>WebRTC Stream</title>
</head>
<body>
  <h1>Live Stream</h1>
  <video id="video" autoplay playsinline controls></video>
  <script>
    const video = document.getElementById("video");
    const serverUrl = "https://8941-93-117-166-235.ngrok-free.app/offer"; // Înlocuiește cu URL-ul public ngrok

    async function start() {
      const pc = new RTCPeerConnection();

      pc.ontrack = (event) => {
        video.srcObject = event.streams[0];
      };
      const offer = await pc.createOffer();
      await pc.setLocalDescription(offer);
      const response = await fetch(serverUrl, {
        method: "POST",
        headers: { 
          "Content-Type": "application/json"
        },
        body: JSON.stringify(pc.localDescription),
        mode: "no-cors" // Asigură-te că CORS este permis
      });
      if (!response.ok) {
        console.error("Eroare la trimiterea ofertei");
        return;
      }
      const answer = await response.json();
      await pc.setRemoteDescription(answer);
    }
    start().catch(err => {
      console.error("Eroare în start():", err);
    });
  </script>
</body>
</html>

**I get this error Traceback (most recent call last):
File “/home/andrei/.local/lib/python3.10/site-packages/aiohttp/web_protocol.py”, line 480, in _handle_request
resp = await request_handler(request)
File “/home/andrei/.local/lib/python3.10/site-packages/aiohttp/web_app.py”, line 569, in _handle
return await handler(request)
File “/home/andrei/Desktop/tekwill_ai_project/server.py”, line 65, in offer
await pc.setLocalDescription(answer)
File “/home/andrei/.local/lib/python3.10/site-packages/aiortc/rtcpeerconnection.py”, line 792, in setLocalDescription
t._setCurrentDirection(and_direction(t.direction, t._offerDirection))
File “/home/andrei/.local/lib/python3.10/site-packages/aiortc/rtcpeerconnection.py”, line 259, in and_direction
return sdp.DIRECTIONS[sdp.DIRECTIONS.index(a) & sdp.DIRECTIONS.index(b)]
ValueError: None is not in list

and i dont know how to get rid of this, i want to create an ngrok server that sends real time video from opencv to a website, can please someone help, it is my first time using aiortc or webrtc?
**

d3 tick format to change unicode to HYPHEN-MINUS (U+002D) for negative values

I realized d3.js returns negative values on xAxis with MINUS unicode (U+2212), but I need it as HYPHEN-MINUS unicode(U+002D). How can I do that within the code below;

    const locale = d3.formatLocale({
      minus: 'u002D',
    })

 const xAxisGroup = d3.select(ref.current)

    xAxisGroup.selectAll('g').remove()

    xAxisGroup
      .append('g')
      .attr('transform', `translate(0,${DEFAULTS.margin.top})`)
      .call(
        d3
          .axisTop(x)
          .tickFormat(locale)
          .ticks(DEFAULTS.width / 80),
      )

But I am not getting what I expected when I add tickFormat(locale) line. I am doing it wrong. Can someone help me about how to apply the format to my xAxis ticks.

Find first non empty array in object array [closed]

So I keep running into this issue trying to use javascripts array.find method. I have a JSON object where im extracting an array of objects that looks like this:

[
  {
    Id: 5303,
    Widgets: [],
  },
  {
    Id: 5302,
    Widgets: [
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object],
      [Object], [Object]
    ],
  }
]

In my case I need to grab the first object where the Widgets array is NOT empty.

So I assumed i’d do something like this:

SomeArray.find((foo) => (foo.Widgets!= undefined))

Since checking length does not work as it’ll give undefined on an empty length array. So I tried adding an || option checking the length (Well that doesn’t work either since it gives null reading of length on empty array)

So how exactly can I return the first object where the widgets array is not empty? It seems to work about half the time because occasionally it’ll return the object where the widgets array is in fact empty.

I can’t find `GatewayIntentBits` in the `discord.js` documentation. Where can I find it?

In the latest versions of discord.js, specifically starting from v13.0.0 and continuing in v14, GatewayIntentBits is an enumeration that contains different intents necessary to interact with Discord’s Gateway API. Intents are used to define what kind of data your bot will receive from Discord, such as message content, member updates, and reactions. It’s important to enable only the necessary intents to reduce overhead and comply with Discord’s privacy policies.

In version 14.16.3 (as of December 2024), GatewayIntentBits is part of the discord.js package and is imported from discord.js. You can use these intents to ensure your bot has access to specific event types that it needs to work properly.

To use it in your bot’s setup, you would initialize the Client with a specific set of intents. Here’s an example demonstrating how to use GatewayIntentBits to create a simple bot:

Example:

const { Client, GatewayIntentBits } = require('discord.js');

// Initialize the Discord client with specific intents
const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,                // Access guilds (servers)
        GatewayIntentBits.GuildMembers,          // Access member updates
        GatewayIntentBits.GuildMessages,         // Access message events
        GatewayIntentBits.MessageContent,        // Access the message content
    ]
});

// Log when the bot is ready
client.once('ready', () => {
    console.log('Bot is online!');
});

// Log the content of a message
client.on('messageCreate', message => {
    if (message.content === '!hello') {
        message.reply('Hello! I am here.');
    }
});

// Log in with your bot token
client.login('YOUR_BOT_TOKEN');

Key Points:

  • GatewayIntentBits: This is an object that holds various intent flags, and you need to specify which intents your bot should have access to.
  • Example Intents:
    • GatewayIntentBits.Guilds – Allows the bot to track guilds (servers).
    • GatewayIntentBits.GuildMembers – Allows the bot to track member updates (like joining or leaving a server).
    • GatewayIntentBits.MessageContent – Allows the bot to access the content of messages sent in servers.

These intents are mandatory for bots that require access to specific data. For example, if you want to track messages, you must request the GatewayIntentBits.GuildMessages intent and possibly MessageContent if you need the content of the messages. Make sure you only request the necessary intents to prevent your bot from being flagged by Discord for over-permissioning.

Where to Find GatewayIntentBits:

You can find it in the official discord.js documentation or GitHub repository for the latest stable version. It’s typically included in the API reference under the ClientOptions section.

Links:

But I can’t find GatewayIntentBits when I search for it in discord.js. Where can I find it?

Handle Arabic letters in search with React

I have a search which return some products from API, these products in Arabic
the Arabic language has many letters looks like the same, for example ( أ – ا ), (ي – ى ), ( ه – ة). I need when I write ه it will search for ( ه and ة) and return the products which contain the ه or ة in table

Example: there’s 5 products containing (ه), and 5 products containing (ة) so I need when I type any letter of them it returns 10 products. How to do this?

I tried this:

const normalizeSearchPhrase = (phrase) => {
    return phrase 
    .replace(/أ|ا/g, "ا") 
    .replace(/ي|ى/g, "ي") 
    .replace(/ة|ه/g, "ه"); 
  };

I expected it to return any product containing أ or ا when I type any of them to get the 10 product but it replace a letter with the other one so it return 5 products not the 10 as example above.

How can I replace JS eval function with more secure function?

How can I replace JS eval function with more secure solution?

const textarea = document.getElementsByTagName("textarea")[0];
const tooltip2 = document.querySelectorAll(".tooltip2");
const hiddenTooltip = document.querySelectorAll(".hiddenTooltip");

let replaced = 'textareaValue';
for (let i = 0; i < tooltip2.length; i++) {
    replaced += '.replace(/' + tooltip2[i].innerText + '/g,' + "'" + hiddenTooltip[i].innerText + "'" + ')'
}
replaced += ';';

textarea.value = eval( replaced );

This code works, eval function changes string to expression, but I read that’s not secure. I should have something like this:

let replaced = textareaValue.replace(/kga/g,'całkowite opracowanie ubytku, selektywne wytrawianie, UB, Gradia A3').replace(/ksc/g,'scaling nad i poddziąsłowy, fluoryzacja na łyżkach').replace(/ksn/g, 'scaling naddziąsłowy, fluoryzacja na łyżkach').replace(/ksp/g, 'scaling poddziąsłowy, fluoryzacja na łyżkach').replace(/kwg/g, 'całkowite opracowanie ubytku, wypełnienie glasjonomerowe');

I have PHP code to add or remove shortcuts dynamically. My PHP code works.

I tried trick with new Function, but it doesn’t work.

textarea.value = new Function('return ' + replaced)();

Could you give me a solution to my problem?

How to properly update reactive array items for the changes to reflect in UI in Vue 3?

I’m relatively new to VUE JS.

I have a menu, opening onclick of a button inside a component – SelectionMatrix.vue
Inside the menu, I have

  1. one select box at the top which has options of 5 different processes, Processa, Processb,Processc, Processd and Processd
  2. 32 checkboxes one below another say banana, apple, orange as in the code.

I have an endpoint which gives me the response below:

{
"processName": "processa",
"debugMask": {
    "processa": "3221225472",
    "processb": "3221225472",
    "processc": "3221225472",
    "processd": "3221225472",
    "processe": "3221225472"
}

}

Here’s what I want to do:

  1. Update the selectedvalue of the selectbox to “processName” from response. In this case – processa
  2. Pick the key value from debugMask, corresponding to the processname selected. In this case – 3221225472
  3. Convert the key value picked into a 32 bit binary string (pre pend with zeros after binary conversion to make it 32) –
    In this case – 11000000000000000000000000000000

In this string, each index corresponds to a bit 1/0 and these decide the whether the checkboxes should be checked – banana and apple checkboxes should be checked

Here’s the code:

    <template>
  <v-menu v-model="menu" :close-on-content-click="false" location="bottom">
    <template v-slot:activator="{ props }">
      <v-btn :color="btnColor" v-bind="props" @click="toggleColor">
        DEBUG SELECTION
      </v-btn>
    </template>

    <v-card min-width="300" class="compact-card" dense>
      <v-list dense>
        <v-list-item dense>
          <v-select
            :items="['Processa', 'Processb', 'Processc','Processd','Processe']"
            v-model="selectedProcess"
            label="Select Process Name"
          ></v-select>

          <v-checkbox
            v-model="selectAll"
            label="Select All"
            @click="selectAllCheckboxes"
            dense
            class="compact-checkbox"
          ></v-checkbox>
        </v-list-item>

        <v-list-item dense v-for="(item, index) in debugOptions" :key="index">
          <v-checkbox
            v-model="item.checked"
            :label="item.label"
            :id="item.id"
            :value="item.value"
            :style="item.style"
            dense
            class="compact-checkbox"
          ></v-checkbox>
        </v-list-item>
      </v-list>

      <v-divider></v-divider>
      <v-row class="justify-end">
        <v-col cols="3" class="ma-3">
          <v-btn
            block
            color="black"
            rounded="2"
            @click="clearTraceMaskChanges()"
            >Clear all
          </v-btn>
        </v-col>
        <v-col cols="3" class="ma-3">
          <v-btn block color="black" rounded="2" @click="selectAll()"
            >Select all
          </v-btn>
        </v-col>
        <v-col cols="3" class="ma-3">
          <v-btn
            block
            color="black"
            rounded="3"
            @click="handleShowConfirmationDialog()"
            >Apply
          </v-btn>
        </v-col>
      </v-row>
    </v-card>
  </v-menu>
  <BaseConfirmationBox
    v-model="showConfirmation"
    @confirm="applyMaskChanges"
    @cancel="handleCancel"
    :message="confirmationMessage"
  />
</template>

<script setup lang="ts">
import { ref, watch} from 'vue'
import { onMounted } from 'vue'
import { storeToRefs } from 'pinia'
import AppUtils from '@/utils/appUtils'
import { useLogsStore } from '../stores/logsStore'
import BaseConfirmationBox from '@/components/BaseConfirmationBox.vue'
const menu = ref(false)
const checkedData = ref([''])
const selectedProcess = ref('Configurator')
const { getTraceMasks, applyTraceMaskChanges } = useLogsStore()
const { traceMaskData } = storeToRefs(useLogsStore())

const showConfirmation = ref(false)
const confirmationMessage = ref('Are you sure?')
const appUtils = new AppUtils()
const btnColor = ref('black')
const selectAll = ref(false)
const debugOptions = ref([
  { id: 'banana', label: 'BANANA', value: 'BANANA', checked: true },
  { id: 'apple', label: 'APPLE', value: 'APPLE', checked: true },
  { id: 'orange', label: 'ORANGE', value: 'ORANGE', checked: true },
  { id: 'grape', label: 'GRAPE', value: 'GRAPE', checked: true },
  { id: 'mango', label: 'MANGO', value: 'MANGO', checked: true },
  { id: 'pineapple', label: 'PINEAPPLE', value: 'PINEAPPLE', checked: true },
  { id: 'unused1', label: 'UNUSED_1', value: 'UNUSED_1', checked: true, style: 'color:blue;' },
  { id: 'kiwi', label: 'KIWI', value: 'KIWI', checked: true },
  { id: 'pear', label: 'PEAR', value: 'PEAR', checked: true },
  { id: 'peach', label: 'PEACH', value: 'PEACH', checked: true },
  { id: 'unused2', label: 'UNUSED_2', value: 'UNUSED_2', checked: true, style: 'color:blue;' },
  { id: 'plum', label: 'PLUM', value: 'PLUM', checked: true },
  { id: 'cherry', label: 'CHERRY', value: 'CHERRY', checked: true },
  { id: 'unused3', label: 'UNUSED_3', value: 'UNUSED_3', checked: true, style: 'color:blue;' },
  { id: 'strawberry', label: 'STRAWBERRY', value: 'STRAWBERRY', checked: true },
  { id: 'unused4', label: 'UNUSED_4', value: 'UNUSED_4', checked: true, style: 'color:blue;' },
  { id: 'blueberry', label: 'BLUEBERRY', value: 'BLUEBERRY', checked: true },
  { id: 'raspberry', label: 'RASPBERRY', value: 'RASPBERRY', checked: true },
  { id: 'blackberry', label: 'BLACKBERRY', value: 'BLACKBERRY', checked: true },
  { id: 'watermelon', label: 'WATERMELON', value: 'WATERMELON', checked: true },
  { id: 'melon', label: 'MELON', value: 'MELON', checked: true },
  { id: 'papaya', label: 'PAPAYA', value: 'PAPAYA', checked: true },
  { id: 'dragonfruit', label: 'DRAGONFRUIT', value: 'DRAGONFRUIT', checked: true },
  { id: 'lychee', label: 'LYCHEE', value: 'LYCHEE', checked: true },
  { id: 'pomegranate', label: 'POMEGRANATE', value: 'POMEGRANATE', checked: true },
  { id: 'fig', label: 'FIG', value: 'FIG', checked: true },
  { id: 'date', label: 'DATE', value: 'DATE', checked: true },
  { id: 'coconut', label: 'COCONUT', value: 'COCONUT', checked: true },
  { id: 'avocado', label: 'AVOCADO', value: 'AVOCADO', checked: true },
  { id: 'guava', label: 'GUAVA', value: 'GUAVA', checked: true },
  { id: 'jackfruit', label: 'JACKFRUIT', value: 'JACKFRUIT', checked: true },
  { id: 'durian', label: 'DURIAN', value: 'DURIAN', checked: true },
]);

const selectAllCheckboxes=() =>{
      debugOptions.value.forEach(option => {
        option.checked = selectAll.value;
      });
    }

const handleShowConfirmationDialog = () => {
  confirmationMessage.value = 'Are you sure you want to apply changes?'
  showConfirmation.value = true
}

const handleCancel = () => {
  confirmationMessage.value = ''
  showConfirmation.value = false
}

function toggleColor() {
  if (btnColor.value === 'black') btnColor.value = 'primary'
  else btnColor.value = 'black'
  getTraceMasks().then(()=>{
    fillCheckBoxData()
  })
}

const fillCheckBoxData = () => {
  let result;
  console.log('TraceMaskData>>>>>>>>>>>>',traceMaskData.value.debugMask)
  console.log('selectedProcess>>>>>>',selectedProcess.value)
  const number = traceMaskData.value.debugMask[selectedProcess.value.toLowerCase()];
  console.log('number for the process>>>>>>>>>>>>>>',number)
    // let number = parseInt(traceMaskData.value[key as keyof typeof traceMaskData.value])
    result = parseInt(number).toString(2)
    console.log("Result>>>>>>>>>>>>", result)
    if (result.length < 32) {
      for (var i = result.length; i < 32; i++) {
        result = '0' + result
      }
    }
    const updatedOptions = debugOptions.value.map((item, index) => {
  return {
    ...item,
    checked: result[index] === '1'
  };
});

debugOptions.value = [...updatedOptions];
console.log('updated checkbox values>>>>>>>>>',debugOptions.value)

}

const tracelogMaskDataTmp = ref({})
tracelogMaskDataTmp.value = traceMaskData



// Watch for changes in the `menu` state to reset button color when menu closes
watch(menu, (newValue) => {
      if (!newValue) {
        btnColor.value = 'black';
      }
    });

onMounted(() => {
  getTraceMasks().then(()=>{
    console.log('Tracemasks value inside component>>>>>>>>>>>>>>>',traceMaskData.value)
    if (traceMaskData.value.debugMask) {
      selectedProcess.value = traceMaskData.value.processName;
      console.log('selected process>>>>>>>>>>>>',selectedProcess.value)
      fillCheckBoxData();
    }
    // selectedProcess.value=traceMaskData.value.processName
    // console.log('selected process>>>>>>>>>>>>',selectedProcess.value)
    // fillCheckBoxData()
  })

})
</script>

<style scoped>
:deep() .v-table .v-table__wrapper > table > tbody > tr > td:not(:last-child),
.v-table .v-table__wrapper > table > tbody > tr > th:not(:last-child) {
  border-right: thin solid rgba(var(--v-border-color), var(--v-border-opacity));
}

:deep() .v-table .v-table__wrapper > table > tbody > tr > td {
  width: 150px !important;
}
:deep(.v-input__details) {
  display: none;
}
.v-card.compact-card :deep(.v-input) {
  --v-input-control-height: 20px; /* Makes the checkbox height smaller */
  --v-input-padding-top: 4px;
  --v-input-padding-bottom: 4px;
}
</style>
  1. The binary conversion is working fine.
  2. console.log(‘updated checkbox values>>>>>>>>>’,debugOptions.value) and the watch works correctly. And I have verified the value of debugOptions
    changing after the GET request.

However this change in debugOptions isn’t reflecting in the UI. The checkboxes aren’t getting checked.
What am I missing here?

How to show specific error message in case of ERR_CERT_AUTHORITY_INVALID

I have a JavaScript app that can connect to various external IoT devices using HTTPS.
The user of the app needs to have the certificate installed from the IoT device in order to connect.

In case the app cannot connect to the device because of a certificate problem, I want to show a specific message in the browser. Though it appears that the error message Axios provides does not disclosure any details (cert problem yes nor no?) about what went wrong.
enter image description here

It basically shows ERR_NETWORK, the same for when the connection timed out.
So this error is not sufficient to identify a certificate problem.

Another idea

I noticed it might be possible to leverage timin, as the above error pops up relatively fast. A normal timeout would take e.g. 10s, whereas the certifcate error pops up almost instantaneous.

But.. I noticed as well that Chrome caches unreachable hosts, meaning timeouts will also occur faster after some tries.

So the question: How is it possbile to make sure Axios is doing its request always and not timeout faster for successive requests? Adding a random number as a query param did not solve.

Missing headers in fetch and XMLHttpRequest

When doing a CORS request using either fetch or XMLHttpRequest, some headers are missing from the response.

To be precise, those APIs drop all headers, that are not explicitely “whitelisted” by the server for cross-origin requests (using Access-Control-Allow-Headers). That I know.

However, if those headers should only be visible when the request is not cross-origin, why does the server send those headers anyway?

What I mean is, most headers are not visible to fetch and xhr, but they are visible to the browser – as evident when having a look at the network tab. It clearly shows all the headers I’d expect in the response, even those not available via the programmatic calls.

Since the headers exist and are sent by the server, couldn’t there be a way to still read those headers programmatically? Why is there this restriction between what the browser sees and what it hands to the APIs?