comparison background.js @ 3:f77dab12bb52

Add support for contextual identities Create new tab with the same contextual identity as the current tab and open the search results in newly created tab. Accessing the tab's cookieStoreId property requires the "cookies" permission.
author Guido Berhoerster <guido+cws@berhoerster.name>
date Thu, 31 Oct 2019 16:22:48 +0100
parents 2050741e9711
children d5f5d016facd
comparison
equal deleted inserted replaced
2:5582a3088b9a 3:f77dab12bb52
101 }); 101 });
102 102
103 browser.menus.onClicked.addListener(async (info, tab) => { 103 browser.menus.onClicked.addListener(async (info, tab) => {
104 let query = typeof info.selectionText !== 'undefined' ? 104 let query = typeof info.selectionText !== 'undefined' ?
105 info.selectionText.trim() : info.linkText.trim(); 105 info.selectionText.trim() : info.linkText.trim();
106 // create a new tab with the same contextual identity as the current tab
107 let newTab;
108 try {
109 newTab = await browser.tabs.create({
110 active: true,
111 cookieStoreId: tab.cookieStoreId,
112 openerTabId: tab.id
113 });
114 } catch (e) {
115 console.log(`Failed to create new tab: ${e.message}`);
116 return;
117 }
106 try { 118 try {
107 await browser.search.search({ 119 await browser.search.search({
108 query: query, 120 query: query,
109 engine: info.menuItemId 121 engine: info.menuItemId,
122 tabId: newTab.id
110 }); 123 });
111 } catch (e) { 124 } catch (e) {
112 console.log(`Failed to search for "${query}": ${e.message}`); 125 console.log(`Failed to search for "${query}": ${e.message}`);
113 } 126 }
114 }); 127 });