边缘扩展中的重定向 [英] Redirection in Edge extension

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

问题描述

我做了一个Edge扩展,它的任务是将页面重定向到特定站点上的另一个站点,当用户单击按钮时,第二个站点将重定向回源站点。 此扩展在本地网络中工作,但有一个小错误。 这两个站点不断地相互重定向。 我在某个地方读到,Edge删除了会话存储和本地存储,以防在本地网络中重定向,所以我尝试了Cookie,但效果不是很好。 在这种情况下,我请求帮助。

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

解决方案

清单.json

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

背景.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的位置,因为它们在mark.json权限数组中应该有更好的位置,但如果是某些URL,它会再次导致乒乓效应。因此,它们保留在onBeforRequestURL数组中。

推荐答案

清单.json

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

背景.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的位置,因为它们在mark.json权限数组中应该有更好的位置,但如果是某些URL,它会再次导致乒乓效应。因此,它们保留在onBeforRequestURL数组中。

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

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