Almost done

This commit is contained in:
EvolvedCode
2016-10-15 14:18:31 +03:00
parent 2c08537550
commit cb877de1ed
4 changed files with 13 additions and 86 deletions

View File

@@ -1,27 +0,0 @@
/*
clear():
* either reloads or clears the page depending on the request
*/
function clear(request, sender, sendResponse) {
switch (request.type){
case "Reload":
location.reload();
break;
case "Blank":
removeEverything();
break;
}
chrome.runtime.onMessage.removeListener(clear);
}
function removeEverything() {
while (document.body.firstChild) {
document.body.firstChild.remove();
}
}
/*
Assign clear() as a listener for messages from the extension.
*/
chrome.runtime.onMessage.addListener(clear);

View File

@@ -19,11 +19,11 @@ html, body {
}
.clear {
background-color: #E74C3C;
background-color: #FBFBC9;
}
.clear:hover {
background-color: #C0392B;
background-color: #EAEA9D;
}
.inactive {

View File

@@ -10,8 +10,7 @@
<div class="button beast">Frog</div>
<div class="button beast">Turtle</div>
<div class="button beast">Snake</div>
<div class="button clear inactive">Reload</div>
<div class="button clear inactive">Blank</div>
<div class="button clear">Reset</div>
<script src="choose_beast.js"></script>
</body>

View File

@@ -1,30 +1,6 @@
/*
Given the name of a beast, get the URL to the corresponding image.
*/
/*
updateButtons():
* looks in the self.beast variable to see if the page is beastified or not,
* updates the buttons with the inactive property if the page is not beastified
* does tho opposite in the case it is beastified
*/
function updateButtons() {
if (self.beast) {
var list = document.getElementsByClassName("clear");
for (var i = 0; i < list.length; i++) {
list[i].className = "button clear";
}
}
else {
var list = document.getElementsByClassName("clear");
for (var i = 0; i < list.length; i++) {
list[i].className = "button clear inactive";
}
}
}
updateButtons();
function beastNameToURL(beastName) {
switch (beastName) {
case "Frog":
@@ -36,24 +12,18 @@ function beastNameToURL(beastName) {
}
}
/*
Listen for clicks in the popup.
If the click is on one of the beast buttons:
The text content of the node is the name of the beast we want.
If the click is not on one of the beasts, return early.
Inject the "beastify.js" content script in the active tab.
Otherwise, the text content of the node is the name of the beast we want.
Then get the active tab and send "beastify.js" a message
containing the URL to the chosen beast's image.
Inject the "beastify.js" content script in the active tab.
Else if the click is on one of the clear buttons:
The text content of the node is the clear method we want.
Inject the "clear.js" content script in the active tab.
Then get the active tab and send "clear.js" a message
containing the clear method.
Then get the active tab and send "beastify.js" a message
containing the URL to the chosen beast's image.
*/
document.addEventListener("click", function(e) {
if (e.target.classList.contains("beast")) {
@@ -67,26 +37,11 @@ document.addEventListener("click", function(e) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {beastURL: chosenBeastURL});
});
self.beast = true;
updateButtons();
return;
}
else if (e.target.classList.contains("clear")) {
var chosenMethod = e.target.textContent;
chrome.tabs.executeScript(null, {
file: "/content_scripts/clear.js"
});
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {type: chosenMethod});
});
self.beast = false;
updateButtons();
chrome.tabs.reload();
return;
}
return;
});