-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
asynchronous data messaging between tabs ftw
- Loading branch information
Showing
7 changed files
with
56 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,44 @@ | ||
let id = 1 | ||
let viewTabUrl = null | ||
|
||
function sendMessagePromise(tabId, item) { | ||
viewTabUrl = chrome.runtime.getURL('index.html?id=' + id++) | ||
|
||
return new Promise((resolve, reject) => { | ||
chrome.tabs.sendMessage(tabId, item, response => { | ||
if (response && response.complete) { | ||
buildTab(response.data) | ||
resolve() | ||
} else { | ||
reject('Something wrong') | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
chrome.browserAction.onClicked.addListener(function () { | ||
// Inject webpack script that finds and processes all SVGs on the page | ||
// Fires as soon as the extension icon is clicked | ||
chrome.tabs.executeScript(null, { file: './dist/gather.js' }) | ||
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { | ||
sendMessagePromise(tabs[0].id, { | ||
message: 'start_gobbling', | ||
}) | ||
}) | ||
}) | ||
|
||
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) { | ||
let targetId = null | ||
const viewTabUrl = chrome.extension.getURL('index.html?id=' + id++) | ||
chrome.tabs.create({ url: viewTabUrl }, function (tab) { | ||
targetId = tab.id | ||
}) | ||
function buildTab(data) { | ||
chrome.tabs.create({ url: viewTabUrl }) | ||
|
||
chrome.tabs.onUpdated.addListener(function listener(tabId, changedProps) { | ||
if (tabId != targetId || changedProps.status != 'complete') return | ||
if (changedProps.status != 'complete') return | ||
|
||
chrome.tabs.onUpdated.removeListener(listener) | ||
|
||
const views = chrome.extension.getViews() | ||
var views = chrome.extension.getViews() | ||
|
||
for (const view of views) { | ||
if (view.location.href == viewTabUrl) { | ||
// This function is running from the window object | ||
// It is being imported from the index.html script src | ||
view.gobble(message) | ||
view.gobble(data) | ||
break | ||
} | ||
} | ||
}) | ||
|
||
return true | ||
}) | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters