Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save changes made by content editable on any website

The following code allows any website to be completely editable temporarily:

document.body.contentEditable = "true";

If I want to save the settings made to a particular website using this method, how can I achieve this using Javascript and if needed PHP, so that next time I visit this URL, the website updates automatically with the settings made. Is this even possible? Is there an extension already available?

like image 472
Elon Musk Avatar asked Jun 15 '18 13:06

Elon Musk


People also ask

What is content editable?

The contenteditable global attribute is an enumerated attribute indicating if the element should be editable by the user. If so, the browser modifies its widget to allow editing.

How do you make an editable content in HTML?

To edit the content in HTML, we will use contenteditable attribute. The contenteditable is used to specify whether the element's content is editable by the user or not. This attribute has two values. true: If the value of the contenteditable attribute is set to true then the element is editable.

How do you make elements editable?

All you have to do is set the contenteditable attribute on nearly any HTML element to make it editable. Here's a simple example which creates a <div> element whose contents the user can edit.

Is Contenteditable safe to use?

Its totally secure. The contenteditable attribute is an enumerated attribute whose keywords are the empty string, true, and false.


2 Answers

Here's a way you can do this using vanilla JS.

Here's JSFiddle since StackOverflow doesn't allow localStorage to be executed on the website as a security feature.

How it works:

  1. Using window.onload = function() {...}, check if 'content' key exists in localStorage
  2. If it does, load the data into <div class="content">
  3. Upon pressing Edit button, contentEditable is toggled
  4. Here you can use any method to save the data. I used content.contentEditable === 'false' to save the innerHTML data to 'content' key.

To Note: localStorage is saved in your browser locally, use databases or anything similar to display edits to all viewers.


// Load content onload if it exists in localStorage
window.onload = function() {
	if(localStorage.getItem('content')) {
		document.querySelector('.content').innerHTML = localStorage.getItem('content');
  }
}

let editBtn = document.querySelector('#edit_content');
let content = document.querySelector('.content');

editBtn.addEventListener('click', () => {
  // Toggle contentEditable on button click
	content.contentEditable = !content.isContentEditable;
  
  // If disabled, save text
  if(content.contentEditable === 'false') {
  	localStorage.setItem('content', content.innerHTML);
  }
});
.content {
  border: 1px solid;
  padding: 15px;
}
<div class="content">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
<br>
<button id="edit_content">Edit</button>
like image 129
Alex Avatar answered Nov 20 '22 08:11

Alex


[Answer basically salvaged from the comments, if it was here would be easier for people to read]

Save the data inside the <div> (not the entire page) into a localStorage. You can save when the document.body.contentEditable is disabled. You can then re-load it into the html when the page reloads.

like image 41
Sheshank S. Avatar answered Nov 20 '22 10:11

Sheshank S.