我如何解决这些问题? [英] How do i solve these issues?
问题描述
它有效,但并不完全如我所料。我有两个问题:1)当某个用户在线,我更改用户ID(通过选项页)来检查另一个,它不会发生,因为它等待45或少了几分钟。我尝试了下面的代码(在options.html中),但它没有帮助。
$ b 2)当我更改用户时,图像输出无法正常工作!它输出前一个用户的图像!!
我如何解决这些问题?
Thanks!
options.html
<脚本>
onload = function(){
if(localStorage.id){
document.getElementById(identifier)。value = localStorage.id;
}
else {
var el = document.createElement(div);
el.innerHTML =输入ID !!;
document.getElementsByTagName(body)[0] .appendChild(el);
}
};
函数onch(){
localStorage.id = document.getElementById(identifier).value;
var bg = chrome.extension.getBackgroundPage();
if(bg.id1){
clearTimeout(bg.id1);
bg.getdata();
}
}
< / script>
< body>
< h1>
< / h1>
< form id =options>
< h2>设定< / h2>
< label>< input type ='text'id ='identifier'value =''onchange =onch()>输入ID< / label>
< / form>
< / body>
< / html>
backg.html
< script type =text / javascript>
var domurl =http://www.xxxxxxxxxxxxxx.xxx/id;
var txt;
var id1;
var id2;
var imgarres = [];
var imgarr = [];
var imgels = [];
$ b $函数getdata(){
if(id1){clearTimeout(id1);}
if(id2){clearTimeout(id2);}
var url = getUrl();
var xhr = new XMLHttpRequest();
xhr.open('GET',url,true);
xhr.setRequestHeader('Cache-Control','no-cache');
xhr.setRequestHeader('Pragma','no-cache');
如果(xhr.readyState == 4){
txt = xhr.responseText;
var r = txt.indexOf('< b class =fl_r> Online< / b>');
var el = document.createElement(div);
el.innerHTML = txt;
var n = imgprocess(el,url);
var nam = el.getElementsByTagName(title)[0] .innerHTML;
if(r!= -1){
var notification = webkitNotifications.createNotification(n,nam,'online !!');
notification.show();
var id1 = setTimeout(getdata,60000 * 45);
}
else {
var id2 = setTimeout(getdata,60000 * 2);
}
}}
xhr.send();
$ b function imgprocess(text,url){
imgels = text.getElementsByTagName(IMG);
for(var i = 0; i< imgels.length; i ++){
if(imgels [i] .src.indexOf(parse(url))!= -1){
imgarr.push(imgels [I]);
$ b(var p = 0; p if(imgarr [p] .parentNode.nodeName ==A){
imgarres.push(imgarr [p]);
}
}
var z = imgarres [0] .src;
return z;
函数getUrl(){
if(localStorage.id){
var ur = domurl + localStorage.id;
返回你的;
}
else {
var notif = webkitNotifications.createNotification(null,'blah,blah,blah','Enter ID in options !!');
notif.show();
getdata();
函数init(){
getdata();
}
< / script>
< / head>
< body onload =init();>
< / body>
< / html>
在选项中而不是 clearTimeout (bg.id1);
try bg.clearTimeout(bg.id1);
对于图像问题,您从来不会清理 imgarres
数组,只添加元素,然后取第一个元素。
PS。您的代码非常难以阅读,也许如果您对它进行了格式化并且没有使用神秘的变量名称,那么您将能够更轻松地找到错误。
UPDATE
我想我知道问题所在。因为 var
关键字,当您设置超时时,您使用的是本地范围变量,因此您的 id1
仅在内部可见这个函数和全局 id1
仍然是 undefined
。所以,而不是:
var id1 = setTimeout(getdata,60000 * 45);
尝试:
id1 = setTimeout(getdata,60000 * 45);
因为 if(bg.id1){} $ c
应该在之后工作但是这并不是必需的,因为您正在清理
bg.clearTimeout(bg.id1); getdata()
中的超时。)
I wrote simplest extension as an exercise in JS coding. This extension checks if some user (of certain social network) is online, and then outputs his/her small image, name and online status in notification alert. It checks profile page every 2 minutes via (setTimeout), but when user becomes "online", i set setTimeout to 45 minutes.(to avoid online alerts every 2 minutes).
It works, but not exactly as i expected. I have 2 issues:
1)When certain user is online and i change user id (via options page) to check another one, it doesnt happen because it waits 45 or less minutes. i tried the following code (in options.html), but it doesnt help.
2)When i change users, image output doesnt work correctly!! It outputs image of previous user!!
How do i fix these problems?? Thanks!
options.html
<script>
onload = function() {
if (localStorage.id){
document.getElementById("identifier").value = localStorage.id;
}
else {
var el = document.createElement("div");
el.innerHTML = "Enter ID!!";
document.getElementsByTagName("body")[0].appendChild(el);
}
};
function onch(){
localStorage.id = document.getElementById("identifier").value;
var bg = chrome.extension.getBackgroundPage();
if(bg.id1){
clearTimeout(bg.id1);
bg.getdata();
}
}
</script>
<body>
<h1>
</h1>
<form id="options">
<h2>Settings</h2>
<label><input type='text' id ='identifier' value='' onchange="onch()"> Enter ID </label>
</form>
</body>
</html>
backg.html
<script type="text/javascript">
var domurl = "http://www.xxxxxxxxxxxxxx.xxx/id";
var txt;
var id1;
var id2;
var imgarres = [];
var imgarr = [];
var imgels = [];
function getdata() {
if (id1){clearTimeout(id1);}
if (id2){clearTimeout(id2);}
var url = getUrl();
var xhr = new XMLHttpRequest();
xhr.open('GET',url, true);
xhr.setRequestHeader('Cache-Control', 'no-cache');
xhr.setRequestHeader('Pragma', 'no-cache');
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
txt = xhr.responseText;
var r = txt.indexOf('<b class="fl_r">Online</b>');
var el = document.createElement("div");
el.innerHTML = txt;
var n = imgprocess(el,url);
var nam = el.getElementsByTagName("title")[0].innerHTML;
if (r != -1) {
var notification = webkitNotifications.createNotification(n, nam, 'online!!' );
notification.show();
var id1 = setTimeout(getdata, 60000*45);
}
else {
var id2 = setTimeout(getdata, 60000*2);
}
}}
xhr.send();
}
function imgprocess(text,url){
imgels = text.getElementsByTagName("IMG");
for (var i=0;i< imgels.length;i++){
if (imgels[i].src.indexOf(parse(url)) != -1){
imgarr.push(imgels[i]);
}
}
for (var p=0; p< imgarr.length; p++){
if (imgarr[p].parentNode.nodeName=="A"){
imgarres.push(imgarr[p]);
}
}
var z = imgarres[0].src;
return z;
}
function getUrl(){
if (localStorage.id){
var ur = domurl + localStorage.id;
return ur;
}
else {
var notif = webkitNotifications.createNotification(null, 'blah,blah,blah', 'Enter ID in options!!' );
notif.show();
getdata();
}
}
function init() {
getdata();
}
</script>
</head>
<body onload="init();">
</body>
</html>
In options instead of clearTimeout(bg.id1);
try bg.clearTimeout(bg.id1);
For image problem looks like you never clean imgarres
array, only adding elements to it and then taking the first one.
PS. You code is very hard to read, maybe if you made it well formatted and didn't use cryptic variable names you would be able to find bugs easier.
UPDATE
I think I know what the problem is. When you are setting the timeout you are using local scope variable because of var
keyword, so your id1
is visible only inside this function and global id1
is still undefined
. So instead of:
var id1 = setTimeout(getdata, 60000*45);
try:
id1 = setTimeout(getdata, 60000*45);
Because of this if(bg.id1){}
inside options is never executed.
(bg.clearTimeout(bg.id1);
should work after that, but it is not needed as you are clearing the timeout inside getdata()
anyway)
这篇关于我如何解决这些问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!