sandoche / Darkmode.js

🌓 Add a dark-mode / night-mode to your website in a few seconds
https://darkmodejs.learn.uno
MIT License
2.72k stars 176 forks source link

Error "Identifier 'darkmode' has already been declared" appears in the console #42

Closed zsdycs closed 4 years ago

zsdycs commented 4 years ago

Hi @sandoche : Thank you very much for Darkmode.js.✔ How exciting it is!I configured it on my personal website.
It adds a lot of fun to the page, and it is very useful at night.❤

But there is currently a problem that bothers me. 😦 The following is a description of the problem.

Code

My code is written like this:

var options = {
  bottom: '64px', // default: '32px'
  right: '32px', // default: '32px'
  left: 'unset', // default: 'unset'
  time: '0.5s', // default: '0.3s'
  mixColor: 'white', // default: '#fff'
  backgroundColor: '#dedede', // default: '#fff'
  buttonColorDark: '#212121', // default: '#100f2c'
  buttonColorLight: '#dedede', // default: '#fff'
  saveInCookies: true, // default: true,
  label: nightAndDaySvg, // default: ''
  autoMatchOsTheme: true // default: true
}

// Set the default value of darkmode
if (window.localStorage.getItem('darkmode') == null) {
  window.localStorage.setItem('darkmode', 'true');
}

const darkmode = new Darkmode(options).showWidget();

Reproduce👻

This error can be reproduced at here.This is a static page.

zsdycs commented 4 years ago

I found a solution.

This can solve this problem:

new window.Darkmode(options).showWidget()

And I think this problem is caused by the browser not canceling the class variable when loading the new page.