Skip to content

Commit

Permalink
WIP: example of sending messages between popup and content script
Browse files Browse the repository at this point in the history
  • Loading branch information
hchiam committed Apr 1, 2020
1 parent c2b4cae commit b05c5be
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 29 deletions.
31 changes: 30 additions & 1 deletion brain.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
clearStorage();
getHosts();

function getHosts() {
const hosts = new Set();
Expand Down Expand Up @@ -43,3 +42,33 @@ function updateStorage(key, data) {
function clearStorage() {
browser.storage.local.clear();
}

// to get messages from popup.js, use this instead of browser.storage.local.get:
browser.runtime.onMessage.addListener((results) => {
const openNewTabs = results.openNewTabs;
if (openNewTabs !== true) return; // just in case

getHosts(); // get hosts now since page has already reloaded after message received

browser.storage.local.get('hosts').then((results) => {
const hosts = results.hosts;
if (hosts === undefined) {
suggestManualForFirst();
} else {
for (const host of hosts) {
openInNewTab(host);
}
}
}, onError);

}); // browser.runtime.onMessage.addListener does not accept onError parameter

function suggestManualForFirst() {
alert("Something went wrong.\n\nTry copying the current page's URL and running a scan here: https://www.urlvoid.com");
openInNewTab();
}

function openInNewTab(host) {
const urlToOpen = 'https://www.urlvoid.com/scan/' + host;
window.open(urlToOpen, '_blank');
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "Check All Scripts with URLVoid",
"description" : "Check all scripts. With 3 clicks. (Disclaimer: This is not an official URLVoid tool. Just an experimental tool that creates and opens links. Recommended to be used in combination with the add-on NoScript.)",
"version": "1.0.5",
"version": "1.0.6",
"homepage_url": "https://github.com/hchiam/urlvoid-firefox-extension",
"icons": {
"48": "icon.png"
Expand Down
2 changes: 1 addition & 1 deletion popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</head>
<body>
<p id="message"></p>
<button id="check" onclick="openInNewTab('google.com')">Check scripts</button>
<button id="check">Check scripts</button>
<script src="popup.js"></script>
</body>
</html>
45 changes: 19 additions & 26 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,35 @@ function onError(error) {
console.log(error);
}

function suggestManualForFirst() {
alert("Something went wrong.\n\nTry copying the current page's URL and running a scan here: https://www.urlvoid.com");
openInNewTab();
}

document.getElementById('check').addEventListener('click', () => {
const yes = confirm('Need to refresh this page.\n\nDo you want to continue?');
if (!yes) return;
waitingStyle();
// send message only after reload
browser.tabs.reload().then(() => {
setTimeout(() => {
browser.storage.local.get('hosts').then((results) => {
const hosts = results.hosts;
// alert(JSON.stringify(results));
if (hosts === undefined) {
suggestManualForFirst();
} else {
for (const host of hosts) {
openInNewTab(host)
}
}
window.close();
}, onError);
}, 1000);
// for popup.js to send data to brain.js, message tabs instead of using browser.storage.local.set:
browser.tabs.query({
currentWindow: true,
active: true
}).then(sendMessageToTabs).catch(onError);
}, onError);
});

function openInNewTab(host) {
const urlToOpen = 'https://www.urlvoid.com/scan/' + host
browser.tabs.create({
url: urlToOpen
});
}

function waitingStyle() {
document.getElementById('check').style.display = 'none';
document.getElementById('message').style.display = 'block';
document.getElementById('message').textContent = 'Just a sec...';
}

function sendMessageToTabs(tabs) {
for (let tab of tabs) {
// instead of using browser.storage.local.set:
browser.tabs.sendMessage(
tab.id,
{
openNewTabs: true
}
).catch(onError);
}
window.close();
}

0 comments on commit b05c5be

Please sign in to comment.