Skip to content

Commit 7610d04

Browse files
committed
Improve tab creation behavior by opening in the current tab index
1 parent 46f8971 commit 7610d04

File tree

2 files changed

+40
-40
lines changed

2 files changed

+40
-40
lines changed

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 2,
33
"name": "Cardtainers",
4-
"version": "1.13",
4+
"version": "1.15",
55
"description": "An experimental way to generate containers for the NationStates trading cards game.",
66
"permissions": [
77
"contextualIdentities",

request.js

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ const icons = ["fingerprint", "briefcase", "dollar", "cart", "circle", "gift", "
33

44
browser.webRequest.onBeforeRequest.addListener(
55
async function (request) {
6-
let puppets = await browser.storage.local.get("puppets")
6+
let puppets = await browser.storage.local.get("puppets");
77
if (puppets && puppets.puppets) {
8-
puppets = JSON.parse(puppets.puppets).split('\n')
8+
puppets = JSON.parse(puppets.puppets).split('\n');
99
}
1010
let url = request.url;
1111
if (url.includes("nation") || url.includes("container")) {
@@ -17,58 +17,58 @@ browser.webRequest.onBeforeRequest.addListener(
1717
for (const segment of segments) {
1818
const [key, value] = segment.split('=');
1919
if (key && value) {
20-
parameters[key] = value;
20+
parameters[key] = value;
2121
}
2222
}
23-
return parameters.container || parameters.nation
24-
}
23+
return parameters.container || parameters.nation;
24+
};
2525
const index = puppets.findIndex((puppet) => puppet.toLowerCase().replaceAll(" ", "_") === parameter());
2626
if (index !== -1) {
27-
const cookieStoreId = await createContainer(puppets[index]);
28-
if (request.cookieStoreId === cookieStoreId) return;
29-
await createNewTabInContainer(url, cookieStoreId, request.tabId );
30-
return {};
27+
const cookieStoreId = await createContainer(puppets[index]);
28+
if (request.cookieStoreId === cookieStoreId) return;
29+
await createNewTabInContainer(url, cookieStoreId, request.tabId);
30+
return { cancel: true };
3131
}
3232
}
33-
return {};
33+
return {};
3434
},
3535
{ urls: ["*://*.nationstates.net/*"], types: ['main_frame'] },
3636
["blocking"]
37-
);
37+
);
3838

3939
async function createContainer(name) {
4040
try {
41-
const find = await browser.contextualIdentities.query({ name });
42-
if (find.length === 0) {
43-
const randomIcon = icons[Math.floor(Math.random() * icons.length)]
44-
const randomColor = colors[Math.floor(Math.random() * colors.length)];
45-
const container= await browser.contextualIdentities.create({
46-
name,
47-
icon: randomIcon,
48-
color: randomColor,
49-
})
50-
return container.cookieStoreId;
51-
} else {
52-
return find[0].cookieStoreId;
53-
}
41+
const find = await browser.contextualIdentities.query({ name });
42+
if (find.length === 0) {
43+
const randomIcon = icons[Math.floor(Math.random() * icons.length)];
44+
const randomColor = colors[Math.floor(Math.random() * colors.length)];
45+
const container = await browser.contextualIdentities.create({
46+
name,
47+
icon: randomIcon,
48+
color: randomColor,
49+
});
50+
return container.cookieStoreId;
51+
} else {
52+
return find[0].cookieStoreId;
53+
}
5454
} catch (error) {
55-
console.error("Error creating or finding contextual identity:", error);
56-
throw error;
55+
console.error("Error creating or finding contextual identity:", error);
56+
throw error;
5757
}
58-
}
58+
}
5959

60-
async function createNewTabInContainer(url, cookieStoreId, originalTabId) {
60+
async function createNewTabInContainer(url, cookieStoreId, originalTabId) {
6161
try {
62-
const activeTab = (await browser.tabs.query({ active: true }))[0];
63-
if (activeTab && activeTab.cookieStoreId === cookieStoreId) {
64-
return;
65-
}
66-
await browser.tabs.create({ url, cookieStoreId, active: false });
67-
if (originalTabId) {
68-
await browser.tabs.remove(originalTabId);
69-
}
62+
const originalTab = await browser.tabs.get(originalTabId);
63+
await browser.tabs.create({
64+
url,
65+
cookieStoreId,
66+
index: originalTab.index,
67+
active: false
68+
});
69+
await browser.tabs.remove(originalTabId);
7070
} catch (error) {
71-
console.error("Error creating tab:", error);
72-
throw error;
71+
console.error("Error creating tab:", error);
72+
throw error;
7373
}
74-
}
74+
}

0 commit comments

Comments
 (0)