Skip to content

Commit

Permalink
Refactored adding profiles so that it saves as JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Delia committed Aug 21, 2020
1 parent a8720f7 commit 62b6b94
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 65 deletions.
2 changes: 2 additions & 0 deletions jquery.min.js

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"content_scripts": [
{
"matches": ["https://*/*", "http://*/*"],
"js": ["options.js"],
"js": [
"jquery.min.js",
"options.js" ],
"run_at": "document_end"
}
],
Expand Down
38 changes: 25 additions & 13 deletions options.html
Original file line number Diff line number Diff line change
@@ -1,32 +1,44 @@
<!DOCTYPE html>
<html>
<head>
<title> Extension Options</title>
<title > Extension Options</title>
<link rel="stylesheet" href="styles.css">
<script src="jquery.min.js"></script>
</head>
<body class = "options">
<div class="header">
<h1 class="title">UpWork Autofill Options</h1>
</div>
<div id="data">
<div class="category">
<legend><span class="number">1</span> Profile 1</legend>
<label for="bio">Cover Letter:</label>
<textarea id="bio" name="user_bio"></textarea>
<legend>
<span class="number">1</span>
<label id="profile0" class="profile">Profile 1</label>
</legend>
<div class="info_option">
<label for="bio0">Cover Letter:</label>
<textarea id="bio0" name="user_bio"></textarea>
</div>
</div>

<div class="category">
<legend><span class="number">2</span> Profile 2</legend>
<label for="sec">Cover Letter 2:</label>
<textarea id="sec" name="user_bio"></textarea>
<legend>
<span class="number">2</span>
<label id="profile1" class="profile">Profile 2</label>
</legend>
<div class="info_option">
<label for="bio1">Cover Letter:</label>
<textarea id="bio1" name="user_bio"></textarea>
</div>
</div>
</div>

<div class="category">
<legend><span class="number">3</span> Profile 3</legend>
<label for="next">Cover Letter 3:</label>
<textarea id="next" name="user_bio"></textarea>
</div>
<div class="header">
<input type="text" id="myInput" placeholder="Add New Profile">
<span id="add_new_profile" onclick="create_new_profile" class="addBtn">Add</span>
</div>

<div id="status"></div>
<button id="add_item">Add New</button>
<button id="save">Save</button>
<script src="options.js"></script>
<script src="popup.js"></script>
Expand Down
134 changes: 89 additions & 45 deletions options.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,101 @@

let n = 4;

// Saves options to chrome.storage
function save_options() {
var bio = document.getElementById('bio').value;
var sec = document.getElementById('sec').value;
var next = document.getElementById('next').value;
var arr=["test"];

chrome.storage.sync.set({
bio: bio,
sec: sec,
next: next,
}, function() {
// Update status to let user know options were saved.
var status = document.getElementById('status');
status.textContent = 'Options saved.';
setTimeout(function() {
status.textContent = '';
}, 750);
});
}
// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
function restore_options() {
chrome.storage.sync.get({
bio: '',
sec: '',
next: ''
}, function(items) {
document.getElementById('bio').value = items.bio;
document.getElementById('sec').value = items.sec;
document.getElementById('next').value = items.next;
console.log("restoring values " + items.sec, items.bio);
});
var profiles = document.getElementsByClassName("category")
for (var i = 0; i < profiles.length; i++) {
var profile = $(`#profile${i}`).text();
var bio = $(`#bio${i}`).val();
var obj = { id: i , name: profile, bio: bio };
arr.push(obj);
}

function add_profile(){
let n = 4
let profile = "Graphics"
let element = `<legend><span class="number">${n}</span> ${profile} </legend> <label >Cover Letter:</label><textarea name="user_bio"></textarea>`;
const position = 'beforeend'
document.getElementById('data').insertAdjacentHTML(position, element);
chrome.storage.sync.set({
bio: '',
list: arr
}, function () {
// Update status to let user know options were saved.
var status = document.getElementById('status');
status.textContent = 'Options saved.';
setTimeout(function () {
status.textContent = '';
}, 750);
});
}

function restore_options() {
console.log("restoring options")
chrome.storage.sync.get({
bio: 'test',
list: arr },
function (data) {
console.log("in options")
console.log(data.list);
console.log(data.bio)
// update(data.list); //storing the storage value in a variable and passing to update function
// function (items) {
// console.log("this is it" + arr);
// items.forEach((item, i) => {
// console.log(item)
// // document.getElementById(`bio${i}`).value = item.bio;
// add_profile(item.bio)
// })
});

function update(array) {
//
// obj = JSON.parse(text);
// document.getElementById("demo").innerHTML += add_element(obj.profile[1], obj.desc[1])
//
array.push("testAdd");
//then call the set to update with modified value
chrome.storage.sync.set({
[items]: ""
}, function(list) {
console.log("restoring values " + list[items]);
document.getElementById('coverLetter').value = list[items]
list: array
}, function () {
console.log("added to list with new values");
});
}
}

//
// function add_element(profile, n) {
// let element = ` <div class="category">
// <legend><span class="number">${n}</span>${profile}</legend>
// <div class="info_option">
// <label for="bio${n}">Cover Letter:</label>
// <textarea id="bio${n}" name="user_bio"></textarea>
// </div>
// </div>`;
// const position = 'beforeend'
// document.getElementById('data').insertAdjacentHTML(element);
// }

function create_new_profile() {
var n = document.getElementsByClassName("category").length + 1;
console.log(n);
var inputValue = document.getElementById("myInput").value;
let element = ` <div class="category">
<legend>
<span class="number">${n}</span>
<label id="profile${n}" class="profile">${inputValue}</label>
</legend>
<div class="info_option">
<label class="profile" for="bio${n}">Cover Letter:</label>
<textarea id="bio${n}" name="user_bio"></textarea>
</div>
</div>`;
document.getElementById('data').insertAdjacentHTML('beforeend', element);
document.getElementById("myInput").value = "";
save_options()
}

$(document).ready(function(){
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('save').addEventListener('click',
save_options);
document.getElementById('add_item').addEventListener('click',
add_profile);
save_options);
document.getElementById('add_new_profile').addEventListener('click', create_new_profile);
})




4 changes: 2 additions & 2 deletions popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</head>
<body>
<div id= "main">
<div class = "front-button popup" id="bio">Web Development</div>
<div class = "front-button popup" id="sec">Graphics Design</div>
<div class = "front-button popup" id="bio0">Software Development</div>
<div class = "front-button popup" id="sec">Web Design</div>
<div class = "front-button popup" id="next">Graphics Design</div>
</div>
</body>
Expand Down
2 changes: 1 addition & 1 deletion popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ function click(e) {
});
chrome.tabs.executeScript(null,
{file:"script.js"});

window.close();
}

document.addEventListener('DOMContentLoaded', function () {
var divs = document.querySelectorAll('div.popup');
console.log(divs)
for (var i = 0; i < divs.length; i++) {
divs[i].addEventListener('click', click);
}
Expand Down
54 changes: 51 additions & 3 deletions styles.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.title{
color: black;
}

body {
overflow: hidden;
margin: 0px;
Expand All @@ -18,6 +22,9 @@ div {
padding: 5px;
}

div#editor {
display: none;
}
*,
*:before,
*:after {
Expand Down Expand Up @@ -135,7 +142,7 @@ label.light {
}
}

.front-button{
.front-button {
color: #FFF;
background-color: #4bc970;
font-size: 18px;
Expand All @@ -153,8 +160,8 @@ label.light {
background-color: #4bc230;
}

.options{
padding: 10%;
.options {
padding: 8%;
overflow: visible;

}
Expand All @@ -166,3 +173,44 @@ label.light {
background: #FFFFFF;
padding: 5px;
}

.editing{
max-width: 20%;
max-height: 20%;
margin: 0px 0px 30px;
height: 58px;
width: 387px;
}

/* Style the "Add" button */
.addBtn {
padding: 15px;
width: 25%;
background: #d9d9d9;
color: #555;
float: right;
text-align: center;
font-size: 16px;
cursor: pointer;
transition: 0.3s;
border-radius: 0;
}

.addBtn:hover {
background-color: #bbb;
}

.profile{
display: inline;
}

/* Style the input */
input {
margin: 0;
border: none;
border-radius: 0;
width: 75%!important;
padding: 10px;
float: left;
font-size: 16px;
}

0 comments on commit 62b6b94

Please sign in to comment.