Recently, some tasks that need to be done every day have always been forgotten. Since the browser will be opened every day when the computer is turned on, I want to make a plug-in to remind myself to do a certain thing at a specific time.
Main file: manifest.json
Code:
{ // This field will be used in the installation dialog, the extension management interface, and the store, the title of the pop-up notification "name": "Desktop Assistant", // The extended version is represented by one to four numbers, separated by dots, must be between 0 and 65535, and non-zero numbers cannot start with 0 "version": "1", // A string describing the extension (cannot be in html or other formats, and cannot exceed 132 characters). This description must be appropriate for both the browser extension's admin interface and the Chrome Web Store. "description": "Desktop Reminder Plugin", // One or more icons to represent extensions, apps, and skins "icons": { "16":"images/32.png", "128": "images/128.png" // used during webstore installation }, // A set of permissions that the extension or app will use "permissions": ["tabs", "notifications"], // Manifest V2 replaces background_page with background property // A Javascript script is specified here "background": { "scripts": ["background.js"] }, // Manifest version 1 has been deprecated in Chrome 18, it should be specified as 2 here "manifest_version": 2 }
js for specific tasks: background.js
Code:
/* created 2017-11-22 by 1540077031 v2.0.3 | (c) 2017 */ //Configure common parameters /* Meaning according to semantics */ var date=getDate(); var json={"moringhour":9,"moringminute":10,"moringsecond":10,"middayhour":11, "middayminute":59,"middaysecond":10,"afternoonhour":17,"afternoonminute":35,"afternoonsecond":10}; //Reminder function showRemind(){ //alert(Boolean(window.Notification)); //Determine whether the browser supports notifications //Welcome notification parameters var options = { type:"image", dir: "ltr", //Control the direction, it is said that the current browser does not support long: "utf-8", icon: "images/18.png", image:"images/oneremind.jpg", body: "The computer has been used for an hour, please take a break", sound:"" }; //create notification object var n = new Notification("Desktop Assistant", options); // show notification n.onshow = function () { setTimeout(n.close.bind(n), 8000); } n.show(); } function showWelcome(){ //alert(Boolean(window.Notification)); //Determine whether the browser supports notifications //Welcome notification parameters var options = { type:"image", dir: "ltr", //Control the direction, it is said that the current browser does not support long: "utf-8", icon: "images/18.png", image:"images/timg1.jpg", body: "Hello, welcome to use the desktop assistant", sound:"9284.wav" }; //create notification object var n = new Notification("Desktop Assistant", options); // show notification n.onshow = function () { setTimeout(n.close.bind(n), 8000); } n.show(); } // morning notification parameters function showMorning(){ var image1=["images/m1.jpg","images/m2.jpg","images/m3.jpg","images/m4.jpg"]; var index1 = Math.floor((Math.random()*image1.length)); var options = { dir: "ltr", //Control the direction, it is said that the current browser does not support long: "utf-8", icon: "images/18.png", image:image1[index1], body: "Good morning, I hope to spend the day in the best condition, come on!!" }; //create notification object var n = new Notification("Desktop Assistant", options); //Close notification periodically n.onshow = function () { setTimeout(n.close.bind(n), 8000); } // show notification n.show(); } //Noon notification parameters function showMidday(){ var image2=["images/d1.jpg","images/d2.jpg","images/d3.jpg","images/d4.jpg","images/d5.jpg","images/d6.jpg","images/d7.jpg","images/d8.jpg","images/d9.jpg","images/d10.jpg","images/d11.jpg","images/d12.jpg","images/d13.jpg","images/d14.jpg","images/d15.jpg"]; var index2 = Math.floor((Math.random()*image2.length)); var options = { dir: "ltr", //Control the direction, it is said that the current browser does not support long: "utf-8", icon: "images/18.png", image:image2[index2], body: "Think about what to eat at noon?", sound:"" }; //create notification object var n = new Notification("Desktop Assistant", options); //Close notification periodically n.onshow = function () { setTimeout(n.close.bind(n), 8000); } // show notification n.show(); } //Notification configuration notification parameters function showNotification(){ var options = { dir: "ltr", //Control the direction, it is said that the current browser does not support long: "utf-8", icon: "images/18.png", image:"images/remind.jpg", body: "Come out to write a daily newspaper"+date, sound:"9284.wav" }; //create notification object var n = new Notification("Desktop Assistant", options); //Close notification periodically n.onshow = function () { setTimeout(n.close.bind(n), 8000); } // show notification n.show(); n.onclick = function() { window.open('http://XXXX.XXX.XXX:9999/in/', '_blank'); } } //Whether the browser supports webkitNotifications if(("Notification" in window)){ timing(); //isFirst();//Every time you open the browser notification } //timed task function timing(){ var go=setInterval(run,1000); } // specific execution method function run(){ var date=new Date(); //json string to object var obj=eval(json); if(date.getHours()==obj.afternoonhour&& date.getMinutes()==obj.afternoonminute && date.getSeconds()==obj.afternoonsecond){ //clearInterval(go); //Terminate the timed task showNotification(); //Notification display in the afternoon }else if(date.getHours()==obj.moringhour&& date.getMinutes()==obj.moringminute && date.getSeconds()==obj.moringsecond){ showMorning(); //Morning notification display }else if(date.getHours()==obj.middayhour&& date.getMinutes()==obj.middayminute && date.getSeconds()==obj.middaysecond){ showMidday(); //Notification display at noon }else{ modifyflag(); } } // Determine if it is the first time to install function isFirst(){ var f=true; //Display cookies for the first installation if(getCookie("flag")=='' && getCookie("flag")!=true){ showWelcome();//Welcome notification display document.cookie="flag="+f; } } // get cookies function getCookie(c_name) { if (document.cookie.length>0) { c_start=document.cookie.indexOf(c_name + "=") if (c_start!=-1) { c_start=c_start + c_name.length+1 c_end=document.cookie.indexOf(";",c_start) if (c_end==-1) c_end=document.cookie.length return unescape(document.cookie.substring(c_start,c_end)) } } return "" } //Get the current time format function getDate(){ var date = new Date(); var year = date.getFullYear(); var month = date.getMonth() + 1; var strDate = date.getDate(); var hour=date.getHours(); var minute=date.getMinutes(); var second=date.getSeconds(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { trDate = "0" + strDate; } if(hour>=0 && hour <=9){ hour="0"+hour; } if(minute>=0 && minute <=9){ minute="0"+minute; } if(second>=0 && second <=9){ second="0"+second; } var currentdate = year +"年"+ month+"月" + strDate+"日" + hour +"时"+ minute +"分"+ second+"秒"; return currentdate; }
There are also some pictures in a folder images
The other operation is to package the test. Of course, everyone's ideas are different, I only provide my own ideas, and everyone can do it according to their own needs.
Project warehouse address: https://github.com/liberliushahe/remind