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?
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.
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.
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.
Its totally secure. The contenteditable attribute is an enumerated attribute whose keywords are the empty string, true, and false.
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:
window.onload = function() {...}
, check if 'content'
key
exists in localStorage
<div class="content">
Edit
button, contentEditable
is toggledcontent.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>
[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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With