前因:javascript
個人電腦本地時間不許了,應該是換塊電池就成,去客服,但客服人員的處理方案是"換主板",擦...我一聽,算了吧,上次我換個主板,就浪費我一個多月的時間,我真心等不起.css
下個工具吧,但許多現成的時間表工具都是讀取本地時間的,因而,只好本身作了個專門獲取網絡時間的桌面小工具.html
正題:java
謝謝網絡上開源代碼的好人們啊json
先貼主要代碼:這個就是桌面工具的主文件api
<!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;網絡
下面按順序app
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); } })();
如下是結構的圖片ide
這是桌面小工具的文件位置:工具
C:\Users\用戶名\AppData\Local\Microsoft\Windows Sidebar\Gadgets
先建個XXX.Gadget文件夾,把代碼文件放裏面,就成小工具了.挺簡單的
效果圖:
***************************************************
功能上確定有缺陷,往後完善
70%的已經完成,後面的還有樣式的優化,和自定義偏好的設置.每一個人有個各自的喜愛,就不貼了