我在JQuery制作了一个秒表,最初是为我的网站制作的,然后我希望它因为各种原因而成为Chrome扩展程序。无论如何,除了一个问题,它运行正常。你启动它,然后一旦弹出窗口关闭它停止,我很痛苦试图弄清楚如何解决这个问题。
您可以在此处下载资源 - http://officialspeed.tk/timesamurai/chrome/stopwatch.zip
清单
{
"name": "TimeSamurai",
"version": "1.0.2",
"description": "A very simple stopwatch with a very nice oriental theme added to it.",
"browser_action": {
"default_icon": "images/32x32.png",
"default_title": "TimeSamurai",
"background_page": "background.html",
"popup": "popup.html"
},
"icons": {
"16": "images/16x16.png",
"32": "images/32x32.png",
"48": "images/48x48.png"
}
}
的style.css
body {
min-width:350px;
min-height:200px;
background-color:black;
background-image:url('images/bg.jpg');
background-position:top left;
background-attachment:scroll;
background-repeat:repeat;}
.display {
position:absolute;
top:25px;
left:50%;
margin-left:-33px;
font-size:18;
font-family:times new roman;
font-weight:bold;
color:FFFFFF;
z-index:1;}
.base {
position:absolute;
top: 50%;
left: 50%;
margin-left: -117px;
margin-top: -50px;
width:235px;}
.start {
position:absolute;
top:3px;
left:50%;
margin-left:-115px;
width:65px;
height:33px;
border:0px;
background-color:transparent;
background-image:url('images/start.png');}
.start:hover {
width:65px;
height:33px;
color:transparent;
border:0px;
background-color:transparent;
background-image:url('images/starthover.png');}
.stop {
position:absolute;
top:3px;
left:50%;
margin-left:-115px;
width:65px;
height:33px;
border:0px;
background-color:transparent;
background-image:url('images/stop.png');}
.stop:hover {
width:65px;
height:33px;
color:transparent;
border:0px;
background-color:transparent;
background-image:url('images/stophover.png');}
.reset {
position:absolute;
top:35px;
left:50%;
margin-left:-115px;
width:65px;
height:32px;
border:0px;
background-color:transparent;
background-image:url('images/reset.png');}
.reset:hover {
width:65px;
height:32px;
color:transparent;
border:0px;
background-color:transparent;
background-image:url('images/resethover.png');}
background.html
<link rel="stylesheet" href="style.css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
(function($) {
jQuery.fn.stopwatch = function() {
var clock = $(this);
var timer = 0;
clock.addClass('stopwatch');
// We have to do some searching, so we'll do it here, so we only have to do it once.
var h = clock.find('.hr');
var m = clock.find('.min');
var s = clock.find('.sec');
var milli = clock.find('.mil');
var start = clock.find('.start');
var stop = clock.find('.stop');
var reset = clock.find('.reset')
stop.hide();
start.bind('click', function() {
timer = setInterval(do_time, 15);
stop.show();
start.hide();
});
stop.bind('click', function() {
clearInterval(timer);
timer = 0;
start.show();
stop.hide();
});
reset.bind('click', function() {
clearInterval(timer);
timer = 0;
h.html("00");
m.html("00");
s.html("00");
milli.html("00");
stop.hide();
start.show();
});
function do_time() {
// parseInt() doesn't work here...
hour = parseFloat(h.text());
minute = parseFloat(m.text());
second = parseFloat(s.text());
millisecond = parseFloat(milli.text());
millisecond++;
if(millisecond > 59) {
millisecond = 0;
second = second + 1;
}
if(second > 59) {
second = 0;
minute = minute + 1;
}
if(minute > 59) {
minute = 0;
hour = hour + 1;
}
h.html("0".substring(hour >= 10) + hour);
m.html("0".substring(minute >= 10) + minute);
s.html("0".substring(second >= 10) + second);
milli.html("0".substring(millisecond >= 10) + millisecond);
}
}
})(jQuery);
$(function() {
$('#timer').stopwatch();
});
</script>
popup.html
<html>
<head>
<link rel="stylesheet" href="style.css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
chrome.extension.getBackgroundPage();
</script>
</head>
<body>
<div class="base">
<img src="images/base.png">
<div id="timer"></div>
</div>
</body>
</html>
答案 0 :(得分:2)
我的猜测是你需要background page。
扩展的常见需求是 有一个长期运行的脚本 管理一些任务或状态。背景 拯救的页面。
在您的扩展程序中,您将使用背景页面来存储和管理秒表的当前状态,并使用弹出页面显示任何UI /进度。
脚本之间有多种通信方式。您可以使用message passing在popup.html / background.html之间进行交谈,或使用chrome.extension.getBackgroundPage()
返回背景页面的dom窗口对象。