JavaScript code that allows users to select text on a website

As I work on developing a chrome extension, my goal is to allow users to select text. I successfully achieved this by running the code in the chrome console.

document.onselectstart=new Function ("return true");

However, when I attempted to incorporate the code into the document ready event, it did not have any effect.

$(document).ready(function(e){
    document.onselectstart=new Function ("return true");
});

I'm now left wondering where exactly should I place this line of code to make it function properly?

Answer №1

It seems like the issue lies in your unintended use of eval: transforming a string into a function.

Unfortunately, this method does not comply with the default Content Security Policy for extensions, although it may still function in the console due to different restrictions.

Instead of resorting to overriding CSP rules with unsafe-eval, consider utilizing anonymous functions like so:

$(document).ready(function(e){
    document.onselectstart = function(){return true;};
});

Answer №2

After much troubleshooting, I finally cracked the code and realized that the event wasn't being triggered (for reasons unknown).

By dynamically adding the script, the issue was resolved.

let customScript = document.createElement('script');
customScript.type='text/javascript';
customScript.innerHTML = "document.onselectstart = function(){return true;};";
let mainBody = document.getElementsByTagName('body')[0];
mainBody.appendChild(customScript);

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

Automatic placement of the cursor at the left end of the username field in the registration form is required

Hey there! I'm looking to customize the registration form on my WordPress website so that when a user clicks on any field, such as the username field, the cursor automatically moves to the left end of that field and the keyboard switches to ENG-US. ...

Emulate mobile view using Javascript and HTML simulation tool

I am trying to simulate the appearance of a website on a mobile phone. For example, consider the following code: <div> <div class = "col-md-6 col-sm-6 col-xs-12"> First </div> <div class = "col-md-6 col-sm-6 col-xs- ...

What is the method for accessing a local XML file with $.ajax?

Currently, I am working on developing a Firefox extension that requires reading a local XML file. However, I have encountered an issue with using $.ajax to read the file. Here is a snippet of my code: $.ajax({ type: "GET", url: "file:/// ...

Determining the distance between points in an STL file with three.js

I'm currently working on a feature that requires users to click on two different points and calculate the distance between them. However, it seems like the clicks are occurring at random positions, resulting in inaccurate calculations. The calculati ...

Difficulty encountered in assigning a value to a variable when utilizing an async function in Node.js

While attempting to retrieve the jwtauthtoken from the user and passing it to a function called getUserId() in the imported JavaScript file, I encountered an issue where the returned value was undefined instead of the decoded ID. The getUserId() function ...

extract the ng-model data and send it to an angular directive

In my current project, I have a specific situation where there are multiple text-boxes and when any of them is clicked, the corresponding ng-model should be displayed in the browser console. To achieve this functionality, I came up with the following Angul ...

Learn how to dynamically switch the background image of a webpage using a button in AngularJS

Hey there, I'm currently working on incorporating interactive buttons into my website to give users the ability to customize the background. I've been experimenting with Angular to achieve this feature. So far, I've managed to change the ba ...

The copyFileSync function is failing to copy the file without generating any error messages

I have developed a JavaScript function running in a nodejs/Electron client to copy a file from the user's flash drive to c:/Windows/System32. The file is copied to enable manual execution from Command Prompt without changing directories. The issue I ...

Rows on bootstrap do not have columns in alignment

Something peculiar is happening to me. I have the following snippet of HTML code, but the issue lies in the fact that the panelPrincipal begins hidden with the following JavaScript code: var panelPrincipal = document.getElementById('panelPrincipal&apo ...

Error encountered in Next.js Webviewer during build process: "Reference Error - window is not defined"

Currently, I am in the process of developing a website that includes a PDF viewer within a dynamically imported page. When I run the code locally, everything works without any issues. However, when I execute the "npm run build" command, I encounter the fol ...

Vue: event triggers malfunctioning and components unresponsive

I am new to Vue.js and I'm attempting to trigger an event from my grand-child component (card) to the child component (hand) and then to the parent component (main): card (emit play event) => hand (listen for play event and emit card-play event) ...

Steps for generating a div, link, and image that links to another image using Javascript

Hey there, I have a cool picture to share with you! <img src="cards.png" id="img"> <!--CARD PICTURE--> Check out what I want to do with it: <div class="container_img"> <img src="cards.png" id="img"> <!--CARD PICTURE--> ...

When attempting to embed Ruby code within JavaScript, Ruby is not being acknowledged or

I am facing an issue with accessing values of a hash created by a ruby function inside javascript. This is the code snippet from my controller: class TransferController < ApplicationController def index require 'json' #@t ...

What is the most efficient way to retrieve a substantial volume of data from Mongodb quickly, especially when the query results require processing before transmitting to the client?

My project involves a large Mongodb database paired with a React front-end and Node back-end, housing around 100000 documents. Indexing has been completed and Redis is already in use. The search feature implemented allows users to find relevant documents b ...

Pressing the space bar invokes an href using AngularJS (ui-router)

How can I handle the space bar key event in Angular? Here are some possible solutions: ng-keyup="$event.keyCode == 32 ? '/settings' : null" ng-keyup="$event.keyCode == 32 ? '#/settings' : null" ng-keyup="$event.keyCode == 32 ? $eval( ...

Utilizing attributes as scope properties within AngularJS

I am currently working on a directive and I need to pass the Attributes (Attrs) to the $scope, however, I am facing some difficulties in achieving this. Specifically, my goal is to assign attributes in my template based on the name set in my date-picker ta ...

Transmit an array of JavaScript objects using Email

Within the code layout provided with this post, I have executed various operations in JavaScript which resulted in an array of objects named MyObjects. Each object within MyObjects contains properties for Name and Phone, structured as follows: MyObject ...

Serving pages with Node JS and loading .js files on the client side

Here is a simple JS file that will be familiar to those who have worked with Socket.IO in NodeJS and Express: var express = require('express'), app = express(), server = require('http').createServer(app), io = require(&apos ...

What steps are involved in creating a video playlist with YouTube videos?

Is there a way to create a dynamic video playlist that supports embedded YouTube videos without refreshing the page? If a user clicks on another video, I want the video to change dynamically. You can check out this for an example. Do jPlayer, Video.js, Fl ...

AngularJS: Users must click submit button twice for it to function properly

It's puzzling that I have to click the submit button on my form twice before it triggers the save function below. My HTML is written in Pug format. Below is a snippet of my HTML template: <form ng-submit="save()"> <div class="form-group" ...