在5000秒的计时器后刷新并打印警报 [英] Refresh after a timer of 5000 seconds and print the alert

查看:124
本文介绍了在5000秒的计时器后刷新并打印警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作Chrome扩展程序,以保持刷新页面,除非停止按钮被选中。但我只能做到一次。这里是我的代码background.js

  chrome.extension.onMessage.addListener(function(request,sender,sendResponse){
switch(request.type){
casetable-row-count_start:
alert(Refershing started);
RefreshAndCount();
break;
casetable-row-count_stop:
alert(Stop Refershing);
break;
}
return true;
});
$ b $ var RefreshAndCount = function(){
chrome.tabs.query({active:true,currentWindow:true},function(tabs){
chrome.tabs.sendMessage( tab [0] .id,{type:table-row-count});
chrome.browserAction.setBadgeText({tabId:tabs [0] .id,text:Counting!});
});
};

在content.js中,我这样做了:

  chrome.extension.onMessage.addListener(function(message,sender,sendResponse){
alert(message.type);
switch(message.type){
casetable-row-count:
var x = document.querySelector('table')。rows.length;
chrome.storage.sync.set({'value': x),function(){
console.log('Settings saved');
});
chrome.storage.sync.get([value],function(items) {
console.log(items);
});
alert(Row count =+ x);
setTimeout(function(){
location。 reload();
},100);

break;
}
});

chrome.storage.onChanged.addListener(function(changes,namespace){
for(key in changes){
if(key =='value'){
var storageChange = changes [key];
console.log('命名空间'%s'中的存储键%s已更改'+
'旧值为%s,新值是'%s'。',
键,
命名空间,
storageChange.oldValue,
storageChange.newValue);
}
}
});

刷新后,我想每次打印当前行数警报。请帮助如何做到这一点。



这个工作很好,只需要一次刷新,但之后我不得不从popup中选择开始按钮。



我想要一些我不需要再次单击开始按钮并重复整个过程,将之前的行数存入缓存或其他东西的地方。

popup.js

  window.onload = function(){ 
document.getElementById(mystartbutton)。onclick = function(){
chrome.extension.sendMessage({
type:table-row-count_start
});

document.getElementById(mystopbutton)。onclick = function(){
chrome.extension.sendMessage({
type:table-row-count_stop
});


同时帮助我如何继续引用那页,即使我切换到其他选项卡或最小化我的铬? 您可以使用chrome.storage .local用于存储将随着时间的推移而保存的数据,以及在您使用它的上下文中保存的数据。您可以将布尔值设置为true或false以启用或禁用自动重新加载。然后你只需要点击浏览器动作就可以设置它,并在内容脚本中检查它是否需要重新加载。



一个可能的和简单的实现应该是像这样:(这取决于预期的行为)



content.js (必须注入页面才能自动重载)

  var reloadDuration = 5000; 
$ b函数autoreload()
{
chrome.local.storage.get(autoreload_enabled,function(result)
{
if(result。 autoreload_enabled)
{
chrome.runtime.sendMessage({type:table-row-count});
window.location.reload();
}
}
}

setTimeout(autoreload,reloadDuration);

如果名为 autoreload_enabled 的chrome本地存储中的布尔值设置为true,则会每< reloadDuration 重新加载页面。


I am making a chrome extension to keep refreshing a page unless stop button is chosen. But i am able to do it only once. Here is my code for background.js

chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
switch(request.type) {
    case "table-row-count_start":
        alert("Refershing started");
        RefreshAndCount();
        break;
    case "table-row-count_stop":
        alert("Stop Refershing");
        break;
}
return true;
});

var RefreshAndCount = function() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    chrome.tabs.sendMessage(tabs[0].id, {type: "table-row-count"});
    chrome.browserAction.setBadgeText({tabId: tabs[0].id, text: "Counting!"});
});
};

In content.js I did this :

chrome.extension.onMessage.addListener(function(message, sender, sendResponse) {
alert(message.type);
switch(message.type) {
    case "table-row-count":
        var x = document.querySelector('table').rows.length;
        chrome.storage.sync.set({'value': x}, function() {
            console.log('Settings saved');
        });
        chrome.storage.sync.get(["value"], function(items){
           console.log(items);
        });
        alert("Row count = " + x);
        setTimeout(function(){
            location.reload();
        },100);

        break;
}
});

chrome.storage.onChanged.addListener(function(changes, namespace) {
    for (key in changes) {
        if(key=='value'){
            var storageChange = changes[key];
            console.log('Storage key "%s" in namespace "%s" changed. ' +
                  'Old value was "%s", new value is "%s".',
                  key,
                  namespace,
                  storageChange.oldValue,
                  storageChange.newValue);
    }
}
});

After refresh I want to print the current row count alert everytime. Please help how to do this .

This work fine, for a single refresh but after that I again had to choose the start button from popup.

I want some way that I need not click start button again and the whole process repeats, storing the previous row count in cache or something.

popup.js

window.onload = function() {
document.getElementById("mystartbutton").onclick = function() {
    chrome.extension.sendMessage({
        type: "table-row-count_start"
    });
}
document.getElementById("mystopbutton").onclick = function() {
    chrome.extension.sendMessage({
        type: "table-row-count_stop"
    });
}
}

Also help me How to keep on refershing that page even if I switch to other tab or minimise my chrome ?

解决方案

You can use the chrome.storage.local to store data that will be saved over time and over context where you use it. You can set a boolean to true or false to enable or disable autoreload. Then you only have to set it at click on browser action and check it in the content-script to know if you have to reload.

A possible and simple implemtation should be like this : (It depends of the expected behavior)

content.js (have to be injected in the page to autoreload)

var reloadDuration = 5000;

function autoreload()
{
    chrome.local.storage.get("autoreload_enabled", function(result)
    {
        if(result.autoreload_enabled)
        {
            chrome.runtime.sendMessage({type: "table-row-count"});
            window.location.reload();
        }
    }
}

setTimeout(autoreload, reloadDuration);

This will reload your page every reloadDuration if the boolean set in chrome local storage named autoreload_enabled is true.

这篇关于在5000秒的计时器后刷新并打印警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆