根据页面状态重新加载网页

时间:2018-03-26 09:04:15

标签: javascript google-chrome google-chrome-extension google-chrome-app

Google Chrome扩展程序开发的新手。我希望写一个小扩展,根据页面内容自动刷新页面。

我遇到了无法传回信息的问题

这是我的整个代码:

background.js

var started = false;
var tabs = new Array();
chrome.tabs.onRemoved.addListener(function(tabID, removeInfo) {
for (var i = 0; i < tabs.length; i++) {
    if (tabs[i].tabId == tabID) {
        tabs[i].started = false;
        clearTimeout(tabs[i].timer);
        tabs.splice(i)
        break;
    }
}})
chrome.browserAction.onClicked.addListener(function(tab) {
var t = new Object();
chrome.tabs.create({
    'url': 'http://localhost/test'
}, function(tab) {
    t.tabId = tab.id
    t.started = true;
    reload(t.tabId)
})
tabs.push(t)
});
chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        if (request.type == "reload") {
            for (var i = 0; i < tabs.length; i++) {
                if (tabs[i].tabId == sender.tab.id) {
                    reload(tabs[i].tabId)
                    break;
                }
            }
        }   else if (request.type == "ready") {

            for (var i = 0; i < tabs.length; i++) {
                if (tabs[i].tabId == sender.tab.id) {
                    tabs[i].started = false;
                    clearTimeout(tabs[i].timer)
                    break;
                }
            }
        }
    });
function sleep(mseconds) {
var timer = new Date();
var time = timer.getTime();
do
    timer = new Date();
while ((timer.getTime() - time) < (mseconds));
}
var CTimer;

function timer(tabid) {
for (var i = 0; i < tabs.length; i++) {
    if (tabs[i].tabId == tabid) {
        if (tabs[i].started) {
            if (tabs[i].timer != undefined) {
                clearTimeout(tabs[i].timer);
            }
            tabs[i].timer = setTimeout(function() {
                reload(tabid)
                console.log("Delay detected : Timer reload")
            }, 5000);
        }
        break;
    }
}
}
function reload(tabid) {
try {
    chrome.tabs.update(tabid, {
        'url': 'http://localhost/test'
    }, function() {
        sleep(750)
        chrome.tabs.executeScript(tabid, {
            file: "reload.js"
        });
    })
    timer(tabid)
} catch (e) {
    reload()
}
}

以下是reload.js

$(function () {
if (location.href.match("http://localhost/test")) {
    if ($("body").html().match("Server Busy")) {
        chrome.tabs.query({
            active: true,
            currentWindow: true
        }, function (tabs) {
            chrome.tabs.sendMessage(tabs[0].id, {
                type: "reload"
            }, function (response) {
                // console.log(response.farewell);
            });
        });
    } else {
        chrome.tabs.query({
            active: true,
            currentWindow: true
        }, function (tabs) {
            chrome.tabs.sendMessage(tabs[0].id, {
                type: "ready"
            }, function (response) {
                // console.log(response.farewell);
            });
        });
    }
}
});

以下是manifest.json(已更新)

{
"background": {
  "scripts": [ "background.js" ]
},
"browser_action": {
  "default_icon": "icon.png"
},
"content_scripts": [ {
  "js": [ "jquery-1.8.2.min.js","reload.js" ],
  "matches": [ "http://localhost/" ]
} ],
"description": "Avoid Server Busy.",
"icons": {
  "128": "icon.png",
  "16": "icon.png",
  "48": "icon.png"
},

"manifest_version": 2,
"name": "Reload",
"permissions": [ "tabs", "http://localhost/", "background" ],
}

当我执行一些调试时,似乎无法将任何消息传递给onRuntime.addListener

我的目的是不断重新加载网页,直到页面不忙,但脚本现在一直在加载页面而不会停止。

我的代码有什么问题吗?

1 个答案:

答案 0 :(得分:0)

试试这个:reload.js

$(function () {
    if (location.href.match("http://localhost/test")) {
        if ($("body").html().match("Server Loading High")) {
            chrome.tabs.query({
                active: true,
                currentWindow: true
            }, function (tabs) {
                chrome.tabs.sendMessage(tabs[0].id, {
                    type: "reload"
                }, function (response) {
                    // console.log(response.farewell);
                });
            });
        } else {
            chrome.tabs.query({
                active: true,
                currentWindow: true
            }, function (tabs) {
                chrome.tabs.sendMessage(tabs[0].id, {
                    type: "ready"
                }, function (response) {
                    // console.log(response.farewell);
                });
            });
        }
    }
});

background.js

chrome.runtime.onMessage.addListener(

    function (request, sender, sendResponse) {

    if (request.type == "reload") {

        for (var i = 0; i < tabs.length; i++) {
            if (tabs[i].tabId == sender.tab.id) {
                reload(tabs[i].tabId)
                break;
            }
        }

    } else if (request.type == "ready") {

        for (var i = 0; i < tabs.length; i++) {
            if (tabs[i].tabId == sender.tab.id) {
                tabs[i].started = false;
                clearTimeout(tabs[i].timer)
                break;
            }
        }

    }

});