Chrome扩展程序延迟条件 [英] Chrome extension delay condition

查看:114
本文介绍了Chrome扩展程序延迟条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Chrome扩展,它在点击按钮后执行某些操作。
但是我不希望它被滥用,所以我需要它的代码在一段时间后被执行。



我怎样才能用超时的方式来包围这段代码
$ b


$ b

感谢您阅读我的消息! .tabs.getSelected(null,function(tab){
var Mp = tab.url.substring(0,23);
if(Mp =='https://www.example.com' )
{
onWindowLoad();

chrome.extension.onMessage.addListener(function(request,sender){
if(request.action ==getSource )
{
...在这里工作
}
});
}
else
{
message.innerHTML ='< span style =color:#f00>这不是一个有效页面< / span>';
}
});

function onWindowLoad()
{
var message = document.querySelector('#message');
chrome.tabs.executeScript(null,{file:getPagesSource.js});


解决方案

我必须妥协因此,在getSelected之后,我添加了以下行:

  chrome.browserAction.disable(tab.Id); 

禁用操作按钮,因此在脚本将数据发送到服务器时无法单击,因为有了这个扩展,我抓住了标签网址,以便将其存储在我的数据库中。
在ajax响应后加入以下内容:

pre $ if(xhr.readyState == 4)
{
message.innerHTML = xhr.responseText;
chrome.browserAction.enable(tab.Id); ///// <---该行
}

按钮准备就绪再次被点击。

我无法找到在几秒钟内添加特定延迟的方式,这种方式似乎很愚蠢,但它的工作,因为从我的服务器响应的延迟是足够的用于切换操作按钮的两种状态。



有了这个,不过另一个问题出现了,这个问题在不同的问题中写出来。


I have created a chrome extension which does something after its button is clicked. However I dont want it be abused so I need the its code to be executed after some time.

How can I surround this code with a timeout in order to achieve this?

Thank you for reading me!

chrome.tabs.getSelected(null,function(tab) {
var Mp=tab.url.substring(0,23);
if(Mp=='https://www.example.com')
{
onWindowLoad();

chrome.extension.onMessage.addListener(function(request, sender) {
    if (request.action == "getSource")
    {
    ...Working here
    }
});
}
else
{
message.innerHTML='<span style="color: #f00">This is not a valid page</span>';
}
});

function onWindowLoad()
{
var message = document.querySelector('#message');
chrome.tabs.executeScript(null, {file: "getPagesSource.js"});
}

解决方案

I had to make a compromise so just after the getSelected I added the following line:

chrome.browserAction.disable(tab.Id);

It disables the action button thus it cant be clicked while the script sends the data to the server, as with this extension I grab the tab url in order to store it in my database. After the ajax response and adding the following

if(xhr.readyState==4)
{
message.innerHTML=xhr.responseText;
chrome.browserAction.enable(tab.Id); /////<---THAT LINE
}

the button gets ready again to be clicked.

I couldnt find the way to add a specific delay in seconds, this way seems stupid but its working, as the response's delay from my server is enough for switching the 2 states of the action button.

With this, however another problem came up, which Ill write in different question.

这篇关于Chrome扩展程序延迟条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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