【发布时间】:2021-12-24 20:37:32
【问题描述】:
我做了一个边缘扩展,其任务是将页面重定向到特定站点的另一个站点,当用户单击一个按钮时,第二个站点重定向回原点。 此扩展适用于本地网络,但有一个小错误。 这两个站点不断地相互重定向。 在某处我读到 Edge 删除 sessionStorage 和 localStorage 以防本地网络中的重定向,所以我尝试了 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);
}
});
});
解决方案
manifest.json
{
"manifest_version": 2,
"name": "PopUp",
"version": "1.0",
"background": { "scripts": ["background.js"] },
"permissions": ["webRequest", "webRequestBlocking", "cookies", "<all_urls>"]
}
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 权限数组中有更好的位置,但在某些 URL 的情况下,它会再次引起乒乓效应。所以他们留在了 onBeforeRequest urls 数组中。
【问题讨论】:
-
扩展控制台中是否有任何错误消息?如果可能,您能否发布一个重现问题的示例,例如包括 manifest.js。我认为这将有助于解决问题。
-
感谢旭东的帮助。
标签: cookies microsoft-edge session-storage