前因:
我的电脑本地时间不准了,应该是换块电池就成,去客服,但客服人员的处理方案是"换主板",擦...我一听,算了吧,上次我换个主板,就浪费我一个多月的时间,我真心等不起.
下个工具吧,但许多现成的时间表工具都是读取本地时间的,于是,只好自己做了个专门获取网络时间的桌面小工具.
正题:
谢谢网络上开源代码的好人们啊
先贴主要代码:这个就是桌面工具的主文件
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <link href="css/gadget.css" type="text/css" rel="stylesheet" /> </head> <body > <div class="main" id="timestyle"> <div class="shouye"> <div id="bjt" style="margin-top:8px"> <div id="bjt_1"> <div id="swf_clock"></div> <div id="bjt_info"> <div id="time">Loading...</div> <div id="date">Loading...</div> </div> </div> </div> </div> <script type="text/javascript"> if (typeof console == "undefined"){ var console = { log:function(){} }; } var swfLoaded = false; var initTime = undefined; function setFDF(){ swfLoaded = true; //console.log(initTime); if(initTime) setClockTime(initTime); return true; } function setClockTime(time){ if (swfLoaded && swfCtrl) { //console.log("COLK:"+time); //try{ swfCtrl.setTime(time); //}catch(e){} } } </script> <script src="core/clock.js"></script> <script type="text/javascript"> (function(){ var week = '日一二三四五六'; var innerHtml = '{0}:{1}:{2}'; var dateHtml = "{0}月{1}日 周{2}"; var timer = 0; var beijingTimeZone = 8; function format(str, json){ return str.replace(/{(d)}/g, function(a, key) { return json[key]; }); } function p(s) { return s < 10 ? '0' + s : s; } window.baidu_time = function(time){ initTime = time; show(time); timer = setInterval(function(){ time += 1000; show(time); }, 1000); } function getcurrenttime() { var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP.3.0"); xmlhttp.open("GET", "http://bjtime.cn", false); xmlhttp.setRequestHeader("If-Modified-Since", "bjtime"); xmlhttp.send(); var dateStr = xmlhttp.getResponseHeader("Date"); var date = new Date(dateStr); var year = date.getFullYear(); var month = date.getMonth() + 1; var date1 = date.getDate(); var hour = date.getHours(); var minutes = date.getMinutes(); var second = date.getSeconds(); var s=year+"-"+month+"-"+date1+" "+hour+":"+minutes+":"+second; var a=s.split(/[^0-9]/); var d=new Date(a[0],a[1]-1,a[2],a[3],a[4],a[5]); return d.getTime(); } function show(time){ var timeOffset = ((-1 * (new Date()).getTimezoneOffset()) - (beijingTimeZone * 60)) * 60000; var now = new Date(time - timeOffset); document.getElementById('time').innerHTML = format(innerHtml, [p(now.getHours()), p(now.getMinutes()), p(now.getSeconds())]); setClockTime(time); document.getElementById('date').innerHTML = format(dateHtml, [ p((now.getMonth()+1)), p(now.getDate()), week.charAt(now.getDay())]); } function init(){ var elm = document.createElement('SCRIPT'); elm.src = 'http://open.baidu.com/app?module=beijingtime&t=' + new Date().getTime(); alert(new Date().getTime()); document.getElementsByTagName('HEAD')[0].appendChild(elm); } window.baidu_time(getcurrenttime()); })(); </script> </body> </html>
剩下3个文件1个xml,1个css,1个js;
下面按顺序
xml:
<?xml version="1.0" encoding="utf-8" ?> <gadget> <name>TimeBar</name> <version>1.0.0.0</version> <hosts> <host name="TimeSidebar"> <base type="HTML" apiVersion="1.0.0" src="Time.html" /> <permissions>Full</permissions> <platform minPlatformVersion="1.0" /> </host> </hosts> </gadget>
css:
body { margin: 0; width: 103px; height: 108px; font-family: verdana; font-weight: bold; font-size: small; } /* 大小设置,见注* */ #timestyle { vertical-align: middle; text-align: center; overflow: hidden; }
js:
var swfCanvas = document.getElementById("swf_clock"); var swfCtrl = undefined; function getSWFVersion(){ var n = navigator; if (n.plugins && n.mimeTypes.length) { var a = n.plugins["Shockwave Flash"]; if (a && a.description) { return a.description.replace(/([a-zA-Z]|s)+/, "").replace(/(s)+r/, ".") + ".0"; } } else if (window.ActiveXObject && !window.opera) { for (var i = 10; i >= 2; i--) { try { var c = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.' + i); if (c) { return i + ".0.0"; break; } } catch (e) { } } } } function creatFlashfunction(id,url, width, height, fq){ var str = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' width=" + width + " height=" + height + " name='"+fq+"' id='"+id+"'>"; str += "<param name='movie' value=" + url + ">"; str += "<param name=FlashVars value=" + fq + ">"; str += "<param name='menu' value='false'>"; str += "<param name='wmode' value='opaque'>"; str += "<param name='allowfullscreen' value='false'>"; str += "<param name='allowscriptaccess' value='always'>"; str += "<embed id='"+id+"_ff' FlashVars='"+fq+"' src='" + url + "' quality='high' pluginspage='http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width=" + width + " height=" + height + " menu='false' allowfullscreen='false' allowscriptaccess='always' name='flashResult' wmode='opaque'/>"; str += "</object>"; swfCanvas.innerHTML = str; } function getSWF(movieName) { var isIE = navigator.userAgent.indexOf('MSIE') != -1 && !window.opera; if(isIE){ return document[movieName] || window[movieName]; }else { return document[movieName+"_ff"] || window[movieName+"_ff"]; } }; (function(){ var swfVer=getSWFVersion(); if(swfVer==undefined||swfVer.split(".")[0]<8){} else { swfCanvas.className = "swfBox"; creatFlashfunction("bd_swf_clock","http://www.baidu.com/swf/aladdin/clock/clock.swf","64px","64px",""); swfCtrl = getSWF("bd_swf_clock"); setTimeout(function(){ if (!swfLoaded) swfCanvas.style.display = "none"; },5000); } })();
以下是结构的图片
这是桌面小工具的文件位置:
C:Users用户名AppDataLocalMicrosoftWindows SidebarGadgets
先建个XXX.Gadget文件夹,把代码文件放里面,就成小工具了.挺简单的
效果图:
***************************************************
功能上肯定有缺陷,日后完善
70%的已经完成,后面的还有样式的优化,和自定义偏好的设置.每个人有个各自的喜好,就不贴了