- Notifications
You must be signed in to change notification settings - Fork0
mystyle-js is a simple and lightweight JavaScript library, which enables developers to let their website visitors apply a custom css stylesheet.
License
RicoBrase/mystyle-js
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
mystyle-js is a simple and lightweight JavaScript library, which enables developers to let their website visitors apply a custom css stylesheet.
- Download thelatest release
- Drop either the production version (
dist/mystyle.min.js) or the non-minified development version (dist/mystyle.js) into your project folder - Link it in your HTML code (e.g.
<script src="../js/mystyle.min.js"></script>) - Done!
The developer needs to implement his own method of getting the css stylesheet from his users, one method using JavaScriptsFileReader class is shown in thedemo.
To be able to use mystyle-js, you need to initialize a new mystyle-js object:
<scriptsrc="../js/mystyle.min.js"></script><script>varmystyle=newMyStyleJS('your-css-id');</script>
- Note:
your-css-idis just a regular HTML element id, used to identify the added stylesheet later on. This can be any valid HTML id of your choice.
Then you need to load your CSS stylesheet. mystyle-js comes with two different methods to do this.
- Loading the stylesheet from a string (e.g. a read file)
- Load the stylesheet from the page's
window.localStorageorwindow.sessionStorage(Webstorage API)
Constructor of the MyStyleJS class.
idis a HTML id applied to the loaded stylesheet for later recognition.- This is used as the content of a
id=""attribute.
- This is used as the content of a
varmystyle=newMyStyleJS('customstyle-example');
Loads a css stylesheet from thecss parameter.
cssis a css stylesheet as plaintext
mystyle.loadCSSfromString("body { background-color: red; }");
Saves the loaded css from theMyStyleJS instance to the usersWebStorage.
persistentdefines the saving location of the css stylesheet.- If
true, the data will be saved to thelocalStorageand will be kept until it's manually deleted. - If
false, the data will be saved to thesessionStorageand will be deleted, if the browser (or tab) is closed.
- If
mystyle.loadCSSfromString("body { background-color: green;}");mystyle.saveCSS(true):
Loads any saved css stylesheets from the usersWebStorage, prioritizing the (non-persistent)sessionStorage.
mystyle.loadCSS();
Removes any loaded custom css stylesheets from the websiteand from the usersWebStorage.
mystyle.clearCSS();
Actually applies the css stylesheet to the website, if css was loaded manually.
Note:
loadCSSfromString(css: string)already callsapplyCSS(), another call is not needed in this case.
mystyle.css="body { background-color: blue; }";mystyle.applyCSS();
There are two ways to view the demo
- View the most up-to-date demohere (GitHub Pages)
- Download/Clone this repository to your computer and open the
demo/index.htmlfile in your favorite webbrowser.
You use mystyle-js in your project? Feel free to contact me, then I may add you and your project here.
About
mystyle-js is a simple and lightweight JavaScript library, which enables developers to let their website visitors apply a custom css stylesheet.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.