Is it viable to modify the appearance of the elements in a webview?

I need to display a webview in my electron app and modify the appearance of its content. I am open to using JS or CSS to either hide elements with .hide() or visibility: none inside the webview.

Can this be done? I have been having trouble finding an answer. Thank you!

Answer №1

If you're using the most recent version of electron, you might want to give this a shot based on the webview documentation:

Adding CSS

// Injecting CSS into the page
var myWebview = ;// define your webview here
myWebview.insertCSS("body{background:#000}");

Alternatively, you can execute some JavaScript code using executeJavascriptCode

var myWebview = ;// define your webview here
myWebview.executeJavaScript("$('.mySelector').hide();");

In both cases, I recommend reading file contents and passing them as function arguments or appending your files to your webview using executeJavascriptCode. See the example below:

// Appending javascript code 
var scriptPath = __dirname + '/path/to/script.js';
var myWebview  = ;// define your webview here
myWebview.executeJavaScript('document.write(\'<script src="' + scriptPath + '"></script>\');');

// Appending CSS code 
var cssPath   = __dirname + '/path/to/stylesheet.js';
var myWebview = ;// define your webview here
myWebview.executeJavaScript('document.write(\'<link rel="stylesheet" type="text/css" href="' + cssPath + '">\');');

Hopefully this information proves useful.

Best of luck!

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

Showing a notification that is persistent and not dismissible

Hello everyone! I've been struggling with a problem on the RPS project for quite some time now. The issue arises when I try to display a message for a tie in the game, but I can't seem to find a solution to make it disappear when the round is not ...

NodeJS is constantly refreshing the data in the database table

Every day, I use a cron job to scrape data and insert it into a database. However, I'm facing an issue where the database results on the server do not refresh themselves after new data is inserted. I've come across 2 solutions here, but one was ...

Looking to introduce Vue.js into an established SSR website?

Can Vue be used to create components that can be instantiated onto custom tags rendered by a PHP application, similar to "custom elements light"? While mounting the Vue instance onto the page root element seems to work, it appears that Vue uses the entire ...

Real estate for a targeted audience

1. Overview I have a list of selectors that should always apply certain properties. Some selectors require additional properties to be added. I'm struggling to find a way to achieve this without repeating code. 2. Minimal CSS Example (MCVE) 2.1 ...

Tips for sending repeated ajax requests for multiple forms within a step-by-step wizard

I am currently using a plugin to develop a wizard with steps. I have encountered an issue where the ajax call is being triggered on every step transition, leading to infinite calls. To address this problem, I implemented a check to prevent multiple ajax ca ...

Issue with Mockjax: asynchronous form submissions are not being intercepted

Currently, I am facing an issue while using qUnit and mockjax to handle a basic async form submission. It seems like the async POST request is passing through mockjax for some reason. test 'RuleModal closes the modal on a successful form submission e ...

How is it that my initial response appears correct in the first log, but then suddenly changes to a 1?

I've encountered a strange issue where the response appears correctly in the initial log, but later changes to a '1' when console.log(data); is called. The screenshot illustrates the pattern: https://i.sstatic.net/zaXcg.png If you expand ...

Using Angular to sort arrays based on the values in a separate array

I have a unique setup where there is a table containing select options in each row. The value of the select option changes based on the 'dataType' specified for that particular row. Here is an example of my Table Array: { 'name':' ...

Issues with running grunt serve

I'm encountering issues with running my project in WebStorm IDE. When I enter grunt serve, I am faced with the following errors: grunt serve Loading "connect.js" tasks...ERROR >> SyntaxError: C:\Users\TT\Documents\ES\fr ...

Creating an engaging Uikit modal in Joomla to captivate your audience

I need help optimizing my modal setup. Currently, I have a modal that displays articles using an iframe, but there is some lag when switching between articles. Here is the JavaScript function I am using: function switchTitleMod1(title,id) { document.g ...

JavaScript is not being loaded onto the client by Express

I am looking to incorporate requireJS into my client code. Here is my file structure: ProjectRoot |-server.js |-public/ |-index.html |-js/ |-app.js |-lib/ |-require.min.js |-underscore.js |-backbone.js ...

When attempting to print a Bootstrap 4 HTML page in Chrome, the browser headers and footers are being clipped

Currently experiencing an issue with printing an HTML page styled using Bootstrap 4 (Bootstrap 3.3.7 works fine) on Chrome where the header and footer are partly covered up. Take a look at this screenshot to see what I mean - the date, title, and file/url ...

Filling form fields with data from a dynamically created table using jQuery

I am facing an issue with populating the fields in the input box of my modal. The modal appears after clicking the Edit button in a table, and it should populate the fields based on the table row where the button is clicked. The table is generated using jQ ...

failure of svg spinning

I'm currently working with an SVG file and attempting to incorporate a spinning animation similar to loaders and spinners. However, I am facing an issue where the rotating radius is too large and I am struggling to control it effectively. CSS: .image ...

Firebase email functionality unable to locate the user

I recently encountered an error on my website's contact form that uses a Firebase function to handle submissions. The error message from the Firebase logs reads as follows: Error: No user record found for the provided identifier. at FirebaseAuthEr ...

Having difficulty changing the value of a Select element in AngularJS

Struggling to update the select value from AngularJs. Check out my code below: <select ng-model="family.grade" > <option ng-repeat="option in options" value='{{option.id}}'>{{option.text}}</option> </s ...

Encountering Digital Ocean Connection Issue with MongoDB

Initially, my Nodejs with Mongodb connection was working perfectly without any changes in the code. However, I am now facing an issue where I cannot connect to MongoDB. When attempting to connect via Robomongo without using SSH, the connection fails. http ...

What could be the reason I am unable to choose data properties from the dropdown options?

There are two different dropdowns for clothing types and colors. When a type of clothing is selected from the first dropdown, JSON data will fill the second dropdown with options based on the first dropdown selection. After selecting an option from the se ...

Gitlab CI fails to execute npm install successfully

I'm facing an issue while trying to install dependencies for a project hosted on Gitlab. The Runner I am using never seems to complete the installation process and always times out while attempting to fetch the dependencies. Below is my configuration ...

Embed a YouTube video within the product image gallery

Having trouble incorporating a YouTube video into my Product Image Gallery. Currently, the main product photo is a large image with thumbnails that change it. You can see an example on my website here. Whenever I attempt to add a video using the code below ...