Skip to content

Commit

Permalink
update agent to support backup
Browse files Browse the repository at this point in the history
  • Loading branch information
Utsav Krishnan committed Oct 3, 2020
1 parent 3cda0d8 commit b488b85
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.vscode/
server.bin
extra/
.DS_Store
Binary file added chordify-extension
Binary file not shown.
Binary file modified extension/chords/Bb_maj.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified extension/chords/C_maj.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 23 additions & 8 deletions extension/contentScript.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// waitForElementToDisplay(500);
waitForElementToDisplay(500);
function waitForElementToDisplay(time) {
if(window.document.getElementsByClassName("icon-love").length > 0) {
var ele = window.document.getElementsByClassName("icon-love")[0];
// e.removeAttribute("onclick");
// console.log("e", ele);
if(window.document.getElementById("mm") != undefined) {
window.document.getElementById("mm").style.opacity = "0";
console.log("background removed");
return;
}
else {
Expand All @@ -13,11 +12,24 @@ function waitForElementToDisplay(time) {
}
}

waitForBottom(500);
function waitForBottom(time) {
if(window.document.getElementsByClassName("dstubn").length > 0 && window.document.getElementsByClassName("dstubn")[0].childNodes.length > 0) {
window.document.getElementsByClassName("dstubn")[0].childNodes[0].click();
console.log("bottom ad closed");
return;
}
else {
setTimeout(function() {
waitIconRight(time);
}, time);
}
}

// waitIconRight(500);
waitIconRight(2000);
function waitIconRight(time) {
if(window.document.getElementById("play-button") != undefined) {
await delay(10000);
// await delay(10000);
var ele = window.document.getElementById("play-button");
console.log("right-icon");
ele.click();
Expand All @@ -28,4 +40,7 @@ function waitIconRight(time) {
waitIconRight(time);
}, time);
}
}
}

// document.getElementsByClassName("dstubn")[0].childNodes[0].click()
console.log("Content script executed.");
2 changes: 1 addition & 1 deletion extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "Chordify",
"description": "Chordify",
"version": "0.2b",
"version": "0.6b",
"applications": {
"gecko": {
"strict_min_version": "57.0"
Expand Down
22 changes: 22 additions & 0 deletions extension/popup/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ function addNewSong() {
dic[name] = url;
updateStorage();
addUrlToDom(name, url);
backup(url);
} else {
console.log("Duplicate url ", name, url);
document.getElementById('duplicate-content').classList.remove("hidden");
}
});

}

function initialize() {
Expand Down Expand Up @@ -57,3 +59,23 @@ function addUrlToDom(key, url) {
function updateStorage() {
browser.storage.local.set(dic);
}

function backup(url) {
const agentUrl = "http://localhost:5000/save";

var xhr = new XMLHttpRequest();

xhr.open("POST", agentUrl, true);

xhr.onreadystatechange = function () {
if (xhr.readyState == XMLHttpRequest.DONE) {
console.log("DONE", xhr.status);
if (xhr.status == 200) {
console.log("Received Response", xhr.response);
} else
console.log("Is the server working?");
}
}

xhr.send(url);
}
27 changes: 23 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package main

import (
"net/http"
"io/ioutil"
"net/http"
"os"
"os/user"
)

func main() {
http.HandleFunc("/", handleAll)
http.ListenAndServe(":5000", nil)
http.HandleFunc("/save", saveSongList)
http.HandleFunc("/", handleAll)
http.ListenAndServe(":5000", nil)
}

func handleAll(w http.ResponseWriter, r *http.Request) {
Expand All @@ -17,4 +20,20 @@ func handleAll(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Write(body2)
}
}

func saveSongList(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)

usr, err := user.Current()
if err != nil {
panic(err)
}

if f, err := os.OpenFile(usr.HomeDir+"/Music/chordify-extension-backup.txt",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644); err == nil {
defer f.Close()
f.WriteString(string(body) + "\n")
}

}

0 comments on commit b488b85

Please sign in to comment.