Chrome扩展程序定时重定向 [英] Chrome Extension Timed Redirection

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

问题描述

我想要实现的是在后台运行Chrome扩展程序,并且每分钟都会重定向到google.com,然后一分钟后不断重定向到stackoverflow.com等等,直到点击图标为止通过扳手图标大部分的扩展。



然而,我只知道如何使用
window.location.replace( http://google.com);



我仍然在学习如何开发chrome扩展,并为一个简单的东西学习过程。我开始从这个教程学习,并尝试了几件事情,现在我想知道如何得到像这样的东西来处理它在后台运行。

解决方案

你也可以写一个内容脚本,你会注入,但它很容易,我只是将其存储在一个变量。我建议您查看 chrome.tab API,如Google在你的后台页面中:

为开发者记录他们的API非常好。

var REDIRECTION_SCRIPT_A =window.location.href ='http://www.google.com';
var REDIRECTION_SCRIPT_B =window.location.href ='http://bit.ly/m2TXqC';
var toGoogle = true;
var intervalId;

chrome.browserAction.onClicked.addListener(function(){
clearInterval(intervalId);
});

//在当前页面上执行重定向脚本
//请注意,您可以根据其ID选择任何选项卡,方法是将
替换为
以下的函数annoyUser ){
console.log(test);
chrome.tabs.executeScript(null,{code:
(toGoogle?REDIRECTION_SCRIPT_A:REDIRECTION_SCRIPT_B)});
到Google =!toGoogle;
}

//每分钟做一次ad infinitum
intervalId = setInterval(annoyUser,5000);

在您的manifest.json中:

  {
...
permissions:[http:// * / *,tabs],
...
}


What I'm trying to achieve is make a chrome extension run in the background, and every minute it'll redirect to google.com and then a minute later redirect to stackoverflow.com and so on continuously until the icon is clicked by the wrench icon where most of the extensions are.

However I only know on how to redirect a page using window.location.replace("http://google.com");

I'm still learning on how to develop chrome extensions, and just making some simple stuff for a learning process. I started learning from this tutorial, and tried a couple things out, and now I wanna figure out how to get something like this to work with it running in the background.

解决方案

You could also just write a content script that you would inject, but it's easy enough that I just stored it in a variable. I would recommend looking at the chrome.tab API, as Google does a very good job of documenting their API for developers.

In your background page:

var REDIRECTION_SCRIPT_A = "window.location.href='http://www.google.com'";
var REDIRECTION_SCRIPT_B = "window.location.href='http://bit.ly/m2TXqC'";
var toGoogle = true;
var intervalId;

chrome.browserAction.onClicked.addListener(function() {
  clearInterval(intervalId);
});

// Execute redirection script on current page
// Note that you can select any tab based on its ID by replacing
// null below
function annoyUser() {
  console.log("test");
  chrome.tabs.executeScript(null, {code:
    (toGoogle ? REDIRECTION_SCRIPT_A : REDIRECTION_SCRIPT_B) });
  toGoogle = !toGoogle;
}

// Do once a minute ad infinitum
intervalId = setInterval(annoyUser, 5000);

In your manifest.json:

{
  ...
  "permissions": ["http://*/*", "tabs"],
  ...
}

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

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