Skip to content

Commit

Permalink
Improve tab creation behavior by opening in the current tab index
Browse files Browse the repository at this point in the history
  • Loading branch information
Kractero committed May 18, 2024
1 parent 46f8971 commit 7610d04
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Cardtainers",
"version": "1.13",
"version": "1.15",
"description": "An experimental way to generate containers for the NationStates trading cards game.",
"permissions": [
"contextualIdentities",
Expand Down
78 changes: 39 additions & 39 deletions request.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ const icons = ["fingerprint", "briefcase", "dollar", "cart", "circle", "gift", "

browser.webRequest.onBeforeRequest.addListener(
async function (request) {
let puppets = await browser.storage.local.get("puppets")
let puppets = await browser.storage.local.get("puppets");
if (puppets && puppets.puppets) {
puppets = JSON.parse(puppets.puppets).split('\n')
puppets = JSON.parse(puppets.puppets).split('\n');
}
let url = request.url;
if (url.includes("nation") || url.includes("container")) {
Expand All @@ -17,58 +17,58 @@ browser.webRequest.onBeforeRequest.addListener(
for (const segment of segments) {
const [key, value] = segment.split('=');
if (key && value) {
parameters[key] = value;
parameters[key] = value;
}
}
return parameters.container || parameters.nation
}
return parameters.container || parameters.nation;
};
const index = puppets.findIndex((puppet) => puppet.toLowerCase().replaceAll(" ", "_") === parameter());
if (index !== -1) {
const cookieStoreId = await createContainer(puppets[index]);
if (request.cookieStoreId === cookieStoreId) return;
await createNewTabInContainer(url, cookieStoreId, request.tabId );
return {};
const cookieStoreId = await createContainer(puppets[index]);
if (request.cookieStoreId === cookieStoreId) return;
await createNewTabInContainer(url, cookieStoreId, request.tabId);
return { cancel: true };
}
}
return {};
return {};
},
{ urls: ["*://*.nationstates.net/*"], types: ['main_frame'] },
["blocking"]
);
);

async function createContainer(name) {
try {
const find = await browser.contextualIdentities.query({ name });
if (find.length === 0) {
const randomIcon = icons[Math.floor(Math.random() * icons.length)]
const randomColor = colors[Math.floor(Math.random() * colors.length)];
const container= await browser.contextualIdentities.create({
name,
icon: randomIcon,
color: randomColor,
})
return container.cookieStoreId;
} else {
return find[0].cookieStoreId;
}
const find = await browser.contextualIdentities.query({ name });
if (find.length === 0) {
const randomIcon = icons[Math.floor(Math.random() * icons.length)];
const randomColor = colors[Math.floor(Math.random() * colors.length)];
const container = await browser.contextualIdentities.create({
name,
icon: randomIcon,
color: randomColor,
});
return container.cookieStoreId;
} else {
return find[0].cookieStoreId;
}
} catch (error) {
console.error("Error creating or finding contextual identity:", error);
throw error;
console.error("Error creating or finding contextual identity:", error);
throw error;
}
}
}

async function createNewTabInContainer(url, cookieStoreId, originalTabId) {
async function createNewTabInContainer(url, cookieStoreId, originalTabId) {
try {
const activeTab = (await browser.tabs.query({ active: true }))[0];
if (activeTab && activeTab.cookieStoreId === cookieStoreId) {
return;
}
await browser.tabs.create({ url, cookieStoreId, active: false });
if (originalTabId) {
await browser.tabs.remove(originalTabId);
}
const originalTab = await browser.tabs.get(originalTabId);
await browser.tabs.create({
url,
cookieStoreId,
index: originalTab.index,
active: false
});
await browser.tabs.remove(originalTabId);
} catch (error) {
console.error("Error creating tab:", error);
throw error;
console.error("Error creating tab:", error);
throw error;
}
}
}

0 comments on commit 7610d04

Please sign in to comment.