Executing JavaScript code remotely on an open browser tab, whether from a file or as a JavaScript expression: How does it work?

Is there a way to execute JavaScript on an already open browser tab remotely? For instance, if there is a tab open (of a webpage I do not manage) and I want to run a script within that specific tab by somehow sending the script from the command line.

The goal is to create the script in any IDE and then execute it in the context of an existing webpage.

Ideally, each execution would be isolated so that multiple evaluations of const someVar = 1 would not interfere with each other (since a const cannot be redefined).

Answer №1

To enable JavaScript debugging in Chromium based browsers, you can run the following commands remotely:

  1. Start the browser:
    chromium --remote-debugging-port=9222
  2. Retrieve a list of open tabs:
    curl -s -o - http://127.0.0.1:9222/json
  3. Execute script on a specific page using Runtime.evaluate:
    page_id=9EB553A6AE7860E5036BCE836258E22F 
    code='(function() { console.log("Hello, console!"); return 42; })()'
    jq -c -n \
      --arg code "$code" \
      '{id: 0, method: "Runtime.evaluate", "params":{expression: $code}}' \
         | websocat -t - "ws://127.0.0.1:9222/devtools/page/$page_id"
    

For the final step, ensure to send properly escaped JavaScript as a string within a JSON object. In this example, I'm utilizing jq and websocat, but other tools can also be used for the same purpose.

Answer №2

Controlling computers remotely can be done in numerous ways, such as SSH, VNC, TeamViewer, NoMachine, and more. With the exception of SSH, these methods allow for graphical control, enabling users to interact with a remote browser through clicking and typing.

If you're looking to have users expose their computer to you, one option is to develop a browser plug-in that can make fetch requests to your server to retrieve code. However, it's important to note that browser plug-ins come with certain security restrictions that may impact your goals.

For example, there are limitations on what a plug-in can do within the developer console. While some manipulations are feasible, like with plug-ins such as React Developer Tools interacting with browser dev tools, further research is needed to understand these boundaries. Additionally, plug-ins have constraints when it comes to accessing the filesystem, although they can potentially influence it with user consent.

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

Hold off on advancing until the particular text is found in the DOM element

One of the challenges I'm facing is creating a function that waits for specific text to change before returning a value: function getElementText() { while(isLoading()) { } return $('#element').text(); } function isLoading() { ...

Implementing reCAPTCHA in Spring MVC with Ajax

I've been attempting to implement Google reCAPTCHA service with Spring MVC, but I keep encountering this error in the console: http://localhost:8080/spapp/ajaxtest.htm?name=frfr&surname=frfr&phone=edede…nGi3CCrD9GprYZDn8VHc-pN--SK-u_xoRKOrn ...

Transforming a typical JSON file into a parent-child hierarchical JSON structure similar to the one utilized in d3's flare.json file format

My JSON file has a specific structure: { "a": "b", "c": "d", "e": { "f": "g", "h": "i" } } I want to transform it into the following structure: { "name": "Root", "parent": "null", "children": [ { ...

Transforming a decimal number into binary base 2 manually in Javascript without using pre-defined functions

As a newcomer to coding and Javascript, I have been given an assignment that requires me to convert base 10 numbers to binary without using specific built-in methods in Javascript (such as alert(a.toString(16))). The constraints are that I can only use loo ...

Guide to streaming audio files using vue.js

I am attempting to incorporate an audio file into my vue.js project using the following code: import sound from '../../recordings/sound.mp4' const audio = new Audio(sound) audio.play() Although this method works perfectly fine, I have encounter ...

Alter the class names on a div element every 3 seconds using React

Looking to animate the movement of CSS classes sequentially? Imagine starting with an image and after 3 seconds, shifting all classes downward so the last one appears on top, creating a dynamic carousel effect. Any assistance would be greatly appreciated! ...

What is the best way to link labels with input fields located separately in Angular?

Imagine a scenario where labels and form fields are being created in a *ngFor loop, as shown below: app.component.ts export class AppComponent { items = ['aaa', 'bbbbbb', 'ccccccccc'] } app.component.html <div class ...

Challenges with implementing a jQuery sortable table in Laravel

I've integrated tableclothjs and Twitter Bootstrap into my project, but the sortable headers are not appearing as expected. I've checked the JavaScript console for any errors or warnings, but there doesn't seem to be any issue indicated. Wh ...

Exploring the Power of Modules in NestJS

Having trouble with this error - anyone know why? [Nest] 556 - 2020-06-10 18:52:55 [ExceptionHandler] Nest can't resolve dependencies of the JwtService (?). Check that JWT_MODULE_OPTIONS at index [0] is available in the JwtModule context. Possib ...

Sending data to a Sheetlabs API using Axios

I'm currently collaborating with a service called Sheetlabs to transform a Google Sheet into a full-fledged API. Unfortunately, I'm struggling to find useful resources online apart from the Sheetlabs documentation, as it appears to be a relativel ...

Angular Digest Loop for Dynamic Photo Grid Styling

I have a special filter that alters the objects being filtered. However, when I apply ng-style="item.gridSize", it triggers my custom grid algorithm. This algorithm was adapted for my requirements from a source found at this link. angular.module("custom.m ...

Tips for effectively managing relationships in a RESTful API

Exploring the concept of REST architecture through the development of a RESTful service for an 'Issues Tracking Application'. In this application, users, projects, issues, and comments are integral components. The relationships are outlined as f ...

Using Promise to manipulate objects and arrays returned from functions

https://i.stack.imgur.com/jvFzC.png router.get('/', function (req, res, next) { var size = req.params.size ? parseInt(req.params.size) : 20; var page = req.params.page ? req.params.page>0 ? (size&(parseInt(req.params.page)-1)) : ...

Automatically reloading POST request when browser back button is pressed

Our web application is built on Spring MVC and Thymeleaf. When a user lands on the home page with a GET request, they enter 2 parameters and submit a POST request. After successful submission, they are directed to another page (let's call it page2) wi ...

Exploring the possibilities with JavaScript options

I am currently working on a project that involves a dropdown list... to complete unfinished sentences. Unfortunately, I am facing an issue with a message stating Uncaught TypeError: Cannot read property 'options' of null Below is a portion of m ...

Issue with adding a new value to an array within a specific key of an object

Imagine I have this function: const createMenu = () => { const obj = { consumption: [], }; return obj; }; It's a function that, when executed, returns the object { consumption: [] } My goal is to add a key inside that object which is a ...

Issue encountered when utilizing Sequelize and Express for API routing

I'm struggling to troubleshoot the routing issue I'm facing. As a newcomer to Sequelize and Express routing, I would appreciate any guidance. My objective is to retrieve a JSON response from a Sequelize query when accessing the API endpoint &apo ...

Three.js - Illuminated Sprites

I am currently working on a 3D maze game where the walls are rendered as Meshes affected by a PointLight. However, I am encountering an issue with some objects that are implemented as Sprites - they appear fully illuminated, similar to MeshBasicMaterial. ...

What is the correct way to utilize the JSON.parse() function in javascript?

Recently, I've been working with an array printed in JSON format. [{"Name":"John Ranniel","Age":"19","City":"Manila"},{"Contact":"09197875656","Relation":"Sister"}] Unexpectedly, I had to split the JSON array into two parts for some reason. When de ...

When the click event occurs, add a class. After a delay using a timeout function, remove the added

I am currently using Jimdo and I have a section where I can edit all the codes, which is working fine except for one feature. Any assistance would be greatly appreciated! Here is the code that adds a class to an animated SVG image with its status set as d ...