Skip to content

Commit

Permalink
Use containers for the temporary tabs instead of windows
Browse files Browse the repository at this point in the history
We can't create incognito tabs, nor can we create windows with contexts
 so the best we can do atm is create tabs in temporary containers

#17 Some pages are still not getting the proper titles...
  • Loading branch information
LoveIsGrief committed Feb 4, 2018
1 parent d6edecf commit 84b1ea8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 38 deletions.
2 changes: 1 addition & 1 deletion content-scripts/getInformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@ let interval = setInterval(() => {
}
clearInterval(interval);
port.postMessage(message);
}, 100)
}, 100)
80 changes: 44 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ var storage = browser.storage.local;
/**
* Information like the title and favicon
*
* Make an AJAX request to build the DOM and get the title that way.
* Create a new inactive tab with the URL in order to compile information about it.
* Forced to do this because "page-worker.Page" doesn't exist anymore.
* Love it.
* Love it...
*
* @param url
* @returns {Promise}
Expand All @@ -21,12 +21,16 @@ function getInformation(url) {
_finalize();
reject();
}, 5000);
var windowId;
let tabId;
let contextId;

function _finalize() {
clearTimeout(timeoutId);
if (windowId) {
browser.windows.remove(windowId);
if (tabId) {
browser.tabs.remove(tabId);
}
if (contextId) {
browser.contextualIdentities.remove(contextId);
}
}

Expand All @@ -37,42 +41,46 @@ function getInformation(url) {

function onError(error) {
_finalize();
console.error(error);
console.error("getInformationError:", error);
reject();
}

browser.windows.create({
incognito: true,
url: url,
state: "minimized",
type: "detached_panel"
}).then((window) => {
windowId = window.id;
let windowTab = window.tabs[0];

// Work around for https://bugzilla.mozilla.org/show_bug.cgi?id=1397667
// absolutely balls that the object tab isn't accessible yet >_>
setTimeout(() => {

// low-key mute the tab
browser.tabs.update(windowTab.id,{ muted: true})

// Wait for async response from getInformation.js
function onConnect(port) {
if(port.sender.tab.id !== windowTab.id){
return
// Use a temporary context for the tab
browser.contextualIdentities.create({
name: "tmp_links_for_later",
color: "pink",
icon: "fingerprint"
}).then((context) => {
contextId = context.id;
browser.tabs.create({
url: url,
active: false,
cookieStoreId: context.cookieStoreId
}).then((tab) => {
tabId = tab.id;
// Work around for https://bugzilla.mozilla.org/show_bug.cgi?id=1397667
// absolutely balls that the object tab isn't accessible yet >_>
setTimeout(() => {
// low-key mute the tab
browser.tabs.update(tab.id,{ muted: true})

// Wait for async response from getInformation.js
function onConnect(port) {
if(port.sender.tab.id !== tab.id){
return
}
port.onMessage.addListener(onInformation);
browser.runtime.onConnect.removeListener(this);
}
console.log("waiting for information");
port.onMessage.addListener(onInformation);
browser.runtime.onConnect.removeListener(this);
}
browser.runtime.onConnect.addListener(onConnect);
browser.tabs.executeScript(windowTab.id, {
file: "content-scripts/getInformation.js"
}).catch(onError);
}, 500);
browser.runtime.onConnect.addListener(onConnect);
browser.tabs.executeScript(tab.id, {
file: "content-scripts/getInformation.js"
}).catch(onError);
}, 500);

}).catch(onError)
}).catch(onError);

}).catch(onError)
});
}

Expand Down
4 changes: 3 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Links for later",
"version": "1.2.2",
"version": "1.3.0",
"description": "Save links that you want to look at for later.",
"author": "LoveIsGrief",
"applications": {
Expand All @@ -27,7 +27,9 @@
"permissions": [
"<all_urls>",
"activeTab",
"contextualIdentities",
"contextMenus",
"cookies",
"storage",
"tabs"
]
Expand Down

0 comments on commit 84b1ea8

Please sign in to comment.