webNavigation如何使用

本人github

chrome.webNavigation API 提供了一组用于观察和处理浏览器导航事件的方法和事件。这个 API 允许你在页面加载的不同阶段执行代码,例如在页面开始加载、完成加载或出现错误时。

权限

首先,你需要在你的 manifest.json 文件中添加 webNavigationhost permissions 权限:

{
  "permissions": [
    "webNavigation",
    "<all_urls>"
  ]
}

事件监听

然后,在你的背景脚本(background.jsbackground.ts)中,你可以使用 chrome.webNavigation API 的各种事件。

onBeforeNavigate

在导航到指定的页面之前触发。

chrome.webNavigation.onBeforeNavigate.addListener(function(details) {
  console.log("Navigating to:", details.url);
});
onCompleted

在页面导航完成时触发。

chrome.webNavigation.onCompleted.addListener(function(details) {
  console.log("Navigation completed to:", details.url);
});
onErrorOccurred

在导航过程中出现错误时触发。

chrome.webNavigation.onErrorOccurred.addListener(function(details) {
  console.log("Error occurred while navigating to:", details.url, "Error:", details.error);
});

过滤

你还可以使用 url 过滤器来限制事件只在特定网站上触发。

const filter = {
  url: [
    { hostSuffix: 'example.com' }
  ]
};

chrome.webNavigation.onCompleted.addListener(function(details) {
  console.log("Navigation completed to:", details.url);
}, filter);

这样,onCompleted 事件只会在访问 example.com(或其子域)时触发。

获取标签页详情

某些事件(如 onCompleted)在其 details 对象中提供了标签页的 tabId,你可以使用这个 tabId 来获取更多关于该标签页的信息。

chrome.webNavigation.onCompleted.addListener(function(details) {
  chrome.tabs.get(details.tabId, function(tab) {
    console.log("Current tab:", tab);
  });
});

这些只是 chrome.webNavigation API 的基础用法。更多高级用法和详细信息,你可以查阅 Chrome 扩展开发文档

上帝王牌,Web Navigation Description Standard web browsers contain features to move backward and forward among the pages recently visited. One way to implement these features is to use two stacks to keep track of the pages that can be reached by moving backward and forward. In this problem, you are asked to implement this. The following commands need to be supported: BACK: Push the current page on the top of the forward stack. Pop the page from the top of the backward stack, making it the new current page. If the backward stack is empty, the command is ignored. FORWARD: Push the current page on the top of the backward stack. Pop the page from the top of the forward stack, making it the new current page. If the forward stack is empty, the command is ignored. VISIT : Push the current page on the top of the backward stack, and make the URL specified the new current page. The forward stack is emptied. QUIT: Quit the browser. Assume that the browser initially loads the web page at the URL http://www.acm.org/ Input Input is a sequence of commands. The command keywords BACK, FORWARD, VISIT, and QUIT are all in uppercase. URLs have no whitespace and have at most 70 characters. You may assume that no problem instance requires more than 100 elements in each stack at any time. The end of input is indicated by the QUIT command. Output For each command other than QUIT, print the URL of the current page after the command is executed if the command is not ignored. Otherwise, print "Ignored". The output for each command should be printed on its own line. No output is produced for the QUIT command. Sample Input VISIT http://acm.ashland.edu/ VISIT http://acm.baylor.edu/acmicpc/ BACK BACK BACK FORWARD VISIT http://www.ibm.com/ BACK BACK FORWARD FORWARD FORWARD QUIT Sample Output http://acm.ashland.edu/ http://acm.baylor.edu/acmicpc/ http://acm.ashland.edu/ http://www.acm.org/ Ignored http://acm.ashland.edu/ http://www.ibm.com/ http://acm.ashland.edu/ http://www.acm.org/ http://acm.ashland.edu/ http://www.ibm.com/ Ignored
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我想要身体健康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值