backg.html
<script type="text/javascript">
var domurl = "http://www.xxxxxx.xxxx/id";
var txt;
var txt2;
var id1;
var id2;
var imgarres = [];
var imgarr = [];
var imgels = [];
var anchors = [];
var anc = [];
function getdata() {
if (id1){clearTimeout(id1);}
if (id2){clearTimeout(id2);}
getUrl();
var xurl = null;
xurl = localStorage['url'];
var xhr = new XMLHttpRequest();
xhr.open('GET',xurl, 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,xurl);
var nam = el.getElementsByTagName("title")[0].innerHTML;
if (r != -1) {
var notification = webkitNotifications.createNotification(n, nam, 'online!!' );
notification.show();
id1 = setTimeout(getdata, 60000*45);
}
else {
id2 = setTimeout(getdata, 60000*2);
}
}}
xhr.send();
}
function imgprocess(text,url){
imgarres = [];
imgarr = [];
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){
if (isNaN(localStorage.id)){
nicktoid(localStorage.id, function(resul){
localStorage.removeItem("url");
localStorage["url"] = "http://xxxxxxxx.xxxx/id" + resul;
});
}
else {
localStorage.removeItem("url");
localStorage["url"] = domurl + localStorage.id;
}
}
else {
var notif = webkitNotifications.createNotification(null, 'blah,blah,blah', 'specify ID ' );
notif.show();
getdata();
}
}
function nicktoid(nick, callback){
var u = "http://xxxxxxx.xxxx/" + nick ;
var xhr2 = new XMLHttpRequest();
xhr2.open('GET',u, true);
xhr2.setRequestHeader('Cache-Control', 'no-cache');
xhr2.setRequestHeader('Pragma', 'no-cache');
xhr2.onreadystatechange = function() {
if (xhr2.readyState == 4) {
txt2 = xhr2.responseText;
var el2 = document.createElement("div");
el2.innerHTML = txt2;
anchors = el2.getElementsByTagName("a");
for (var i=0;i< anchors.length;i++){
if (anchors[i].parentNode.id == "profile_avatar"){
anc.push(anchors[i]);
}
}
var res = anc[0].href;
var ir = res.replace(/^chrome-extension:\/\/.*?\//, "/");
var arstr = ir.split("_");
var resul = arstr[0].substr(6);
callback(resul);
}
}
xhr2.send();
}
function init() {
getdata();
}
</script>
</head>
<body onload="init();">
</body>
</html>
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){
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>
这篇文章是对previous one的跟进。 我决定通过昵称添加指定用户(除了id)。 问题是:它没有按预期工作。它不会立即通过选项页面更改用户。它(可能)将nick保存到localStorage,但显示以前的用户!! 它只在两次重新加载扩展后开始显示当前用户!!(通过ID指定工作正常!)。我错过了什么?