You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Edge扩展局域网站点双向跳转循环死锁问题及解决方案

Edge扩展局域网环境循环跳转故障解决

问题描述

我开发了一款Edge扩展,功能为访问特定站点时自动跳转到指定站点,用户点击该目标站点按钮后可跳转回原始站点。该扩展在局域网环境运行时出现两个站点持续循环跳转的故障。我曾查阅资料得知Edge在局域网重定向场景下会清除sessionStorage和localStorage,因此切换为使用cookie做标识但未成功解决问题。

初始实现代码

初始的background.js代码如下:

//background.js
const apps = [
  ['AAA', 'aaa.intra.abc.xx']
];

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        const url = tabs[0].url;
        const domain = (new URL(url)).hostname.replace('www.','').toLowerCase();
        
        try {
                chrome.cookies.get({ 'url': tabs[0].url, 'name': domain }, 
                    function(data){
                        if (!data) {                                
                            const i = apps.findIndex(u => domain.includes(u[1]));
                            if (i > -1) {
                                chrome.cookies.set({
                                    url: tabs[0].url,
                                    name: domain,
                                    value: apps[i][0]
                                });    
                        chrome.tabs.update( tabs[0].id, { url: `http://popup.intra.abc.xx?title=${apps[i][0]}`} ); 
                            }
                        }
                    }   
               );
        } catch (e) {
            alert("Error: " + e);
        }
    });
}); 

解决方案

1. manifest.json 配置

{
   "manifest_version": 2,
   "name": "PopUp",
   "version": "1.0",
    "background": { "scripts": ["background.js"] }, 
    "permissions": ["webRequest", "webRequestBlocking", "cookies", "<all_urls>"]
}

2. 修复后 background.js 代码

const apps = [
  ['AAA', 'aaa.intra.abc.xx']
];

function logURL(requestDetails) {
    const domain = (new URL(requestDetails.url)).hostname.replace('www.','').toLowerCase();
    chrome.cookies.get({ 'url': requestDetails.url, 'name': 'status' }, 
        function(data){
            if (data === null) {    
                const i = apps.findIndex(u => domain.includes(u[1]));
                if (i > -1) {
                    chrome.cookies.set({
                        url: requestDetails.url,
                        name: "status",
                        value: "opened"
                    });
                    const url = 'http://popup.intra.abc.xx/?title=' + apps[i][0];   
                    chrome.tabs.update( requestDetails.tabId, { url: url} );
                }
            }
        }   
    );  
}
chrome.webRequest.onBeforeRequest.addListener(
    logURL,
    {urls: ["https://...", "http://.../*", "http://.../*"]},
    ["blocking"]
  );

方案说明

曾尝试将URL规则配置到manifest.json的permissions数组中,但部分URL配置会再次触发循环跳转问题,因此最终将URL规则保留在onBeforeRequest监听的urls参数中。

内容的提问来源于stack exchange,提问作者sada

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 23:54:03