**Problems:**As mobile devices become more and more popular for people to access the Internet, browsing under poor light condition happens more frequently in our daily life. However, CSS of most websites were designed for well lit environment, like dark text with white background. The contrast leads to uncomfortable experience for user to surf the Internet under such circumstance.
Proposal: Here is to propose a solution which provides a way in which web page can be notified of light conditions from browser and change its CSS accordingly. More details please refer to the explainer doc. The following is selectively extracted from the explainer.
API Here is to propose a solution which builds connection between web page and browser side. Browser notifies page of “colormodechanged” event when user switches day/night mode in the UI of browser. Then page side calls the registered event listener to perform changes of style by its own CSS and JS.
- The ColorModeChangedEvent IDL: Noted that the attribute “colortheme” is defined either “normal” or “night” by browser side.
[Constructor(DOMString theme)]
interface ColorModeChangedEvent : Event {
readonly attribute DOMString colortheme;
};
- Due to some browsers have implemented browser-side night mode as mentioned above. Web page has to define a special <meta> to inform browser whether it is using the Night Mode API or not:
<meta name=”x5-colormode” content=”x5-nightmode”>
- (optional) If web page wants to query current “colortheme”:
window.colortheme
- (optional) In case browser takes time in recalculating style, repainting, etc. after the listener was run. A new pseudo-class could be added on some elements and browser would first select the styles in pseudo-class in night mode and performs a quick refreshing. However, the pseudo-class needs to be supported by browser engine.
:-x5-colormode-night
Usage The following is a simple example using Night Mode API. It works in QQBrowser for Android. There is an apk provided in github.
<html>
<head>
<meta name="x5-colormode" content="x5-nightmode"/>
<script>
window.addEventListener('colormodechanged', function (evt) {
if (evt.colortheme === 'night') {
document.documentElement.className += "G-night"
} else if (evt.colortheme === 'normal') {
document.documentElement.className -= "G-night"
}
});
</script>
<style>
.G-night {background-color:yellow;font:black;}
.G-night:-x5-colormode-night {background-color:white;font:red;}
</style>
</head>
<body class="G-night">
Hello World!
</body>
</html>