Chrome 扩展程序定时重定向

发布于 2024-11-13 14:10:38 字数 368 浏览 7 评论 0原文

我想要实现的是让 chrome 扩展在后台运行,每分钟它都会重定向到 google.com,然后一分钟后重定向到 stackoverflow.com 等等,直到用扳手单击图标为止大多数扩展程序所在的图标。

但是我只知道如何使用重定向页面 window.location.replace("http://google.com");

我仍在学习如何开发 Chrome 扩展,只是为学习过程制作一些简单的东西。我从教程开始学习,并尝试了一些方法,现在我想弄清楚如何让这样的东西在后台运行。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话难免假 2024-11-20 14:10:38

替代解决方案(我将 url 选择部分保留)。

背景.html:

var timer = setInterval(function() {
    chrome.tabs.getSelected(null, function(tab) {
        chrome.tabs.update(tab.id, {url: "http://google.com"});
    });
}, 60000);

//stop
chrome.browserAction.onClicked.addListener(function(){
    clearInterval(timer);
});

Alternate solution (I am leaving url selection part out).

background.html:

var timer = setInterval(function() {
    chrome.tabs.getSelected(null, function(tab) {
        chrome.tabs.update(tab.id, {url: "http://google.com"});
    });
}, 60000);

//stop
chrome.browserAction.onClicked.addListener(function(){
    clearInterval(timer);
});
只是偏爱你 2024-11-20 14:10:38

您也可以只编写要注入的内容脚本,但这很简单,我只是将其存储在变量中。我建议您查看 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);
});

// 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);

在您的manifest.json中:

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

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"],
  ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文