Skip to content

Commit

Permalink
add: edit and delete functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mufasa159 committed Jun 1, 2024
1 parent 2db8901 commit 9004a03
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 86 deletions.
27 changes: 21 additions & 6 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
let redirect_link = {}
let redirect_link = {};
chrome.storage.sync.get(['redirect_link'], (result) => {
if (result.redirect_link !== undefined) {
redirect_link = result.redirect_link;
}
});

chrome.omnibox.onInputEntered.addListener((text) => {
if (redirect_link[text] !== undefined){
chrome.tabs.update({url: redirect_link[text]});
if (redirect_link[text] !== undefined) {
chrome.tabs.update({ url: redirect_link[text] });
} else {
chrome.tabs.update({url: "./error.html"});
chrome.tabs.update({ url: "./error.html" });
}
});

Expand All @@ -19,7 +19,22 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
const key = data[0];
const url = data[1];
redirect_link[key] = url;
chrome.storage.sync.set({redirect_link});
chrome.storage.sync.set({ redirect_link });
sendResponse({ message: "Form submitted successfully!" });

} else if (message.type === 'edit_submission') {
const oldKey = message.oldKey;
const newKey = message.newKey;
const newUrl = message.newUrl;
delete redirect_link[oldKey];
redirect_link[newKey] = newUrl;
chrome.storage.sync.set({ redirect_link });
sendResponse({ message: "Edit submitted successfully!" });

} else if (message.type === 'delete_link') {
const key = message.key;
delete redirect_link[key];
chrome.storage.sync.set({ redirect_link });
sendResponse({ message: "Link deleted successfully!" });
}
});
});
9 changes: 4 additions & 5 deletions error.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>All Quick Link</title>
<title>Quick Link - 404</title>
</head>
<body>
<script>

</script>
<p>Sorry, no URL found for the entered keyword.</p>
<p style="text-align:center;margin-top:50px;">
Sorry, no URL found for the entered keyword.
</p>
</body>
</html>
55 changes: 43 additions & 12 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,34 +1,65 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Quick Link</title>
<link rel="stylesheet" href="style.css">
<script src="./background.js"></script>
<script defer src="./popup.js"></script>
</head>

<body>
<h2 class="title">Q-Links</h2>
<p>Add a new link to local storage</p>

<p>Add a new link to storage</p>

<!-- Create Form -->
<form id="user-input" method="POST" enctype="multipart/form-data">
<input type="text" name="quick-link-key" placeholder="Quick Text" required/><br/>
<input type="text" name="quick-link-url" placeholder="Destination URL" required/><br/>
<input type="text" name="quick-link-key" placeholder="Quick Text" required /><br />
<input type="text" name="quick-link-url" placeholder="Destination URL" required /><br />
<div id="alert-success">
<p>Q-link added successfully.</p>
</div>
<div id="alert-error">
<p>Something went wrong</p>
</div>
<button type="submit" id="submit-button">Create</button>
</form>
<br/>
<!-- <a href="./data.html">Edit</a><br/> -->
<a id="get-links">View Storage</a><br/>
</form>

<!-- Storage Container -->
<br />
<a id="get-links">View Storage</a><br />
<div id="storage-container"></div>
<br/>
<small><a href="https://stackoverflow.com/a/37105645/12916568" target="_blank">Click here</a> for more details on when stored data is deleted.</small>

<!-- Edit Form -->
<form id="edit-form" method="POST" style="display: none;">
<input type="text" name="edit-link-key" placeholder="New Quick Text" required /><br />
<input type="text" name="edit-link-url" placeholder="New Destination URL" required /><br />
<button type="submit" id="edit-button">Save</button>
<button type="button" id="cancel-edit-button">Cancel</button>
</form>

<!-- Link Template -->
<template id="link-template">
<div class="link-item">
<div class="top">
<span class="link-key"></span>
<div>
<button class="edit-button">Edit</button>
<button class="delete-button">Delete</button>
</div>
</div>
<span class="link-url"></span>
</div>
</template>

<br />
<small>
<a href="https://stackoverflow.com/a/37105645/12916568" target="_blank">Click here</a> for more details on
when stored data is deleted.
</small>

<script src="popup.js"></script>
</body>

</html>
12 changes: 8 additions & 4 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"name": "Quick Link",
"version": "2.0",
"version": "3.0",
"author": "Mufasa A. (mufasa159)",
"manifest_version": 3,
"description": "Quickly access links using shortened text on your browser's search bar.",
"omnibox": { "keyword" : "q" },
"omnibox": {
"keyword": "q"
},
"background": {
"service_worker": "background.js"
},
"action":{
"action": {
"default_popup": "index.html",
"default_title": "Quick Link"
},
Expand All @@ -18,5 +20,7 @@
"48": "assets/logo_48px.png",
"128": "assets/logo_128px.png"
},
"permissions": [ "storage", "tabs" ]
"permissions": [
"storage"
]
}
146 changes: 101 additions & 45 deletions popup.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,107 @@
const form = document.getElementById("user-input");
const alert_success = document.getElementById("alert-success");
const alert_error = document.getElementById("alert-error");
const storage = document.getElementById("storage-container");
storage.style.display = 'none';

form.addEventListener("submit", (event) => {
event.preventDefault();
const data = [];
const key = form.elements["quick-link-key"].value;
const url = form.elements["quick-link-url"].value;
data.push(key);
data.push(url);
chrome.runtime.sendMessage({ type: 'form_submission', data } , (response) => {
console.log(response.message);
});
alert_success.style.display = 'block';
document.getElementById("user-input").reset();
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById("user-input");
const alert_success = document.getElementById("alert-success");
const storage = document.getElementById("storage-container");
const editForm = document.getElementById("edit-form");
const editButton = document.getElementById("edit-button");
const cancelEditButton = document.getElementById("cancel-edit-button");

storage.style.display = 'none';
storage.innerHTML = "";
});
editForm.style.display = 'none';

const getLinks = document.getElementById('get-links');
getLinks.addEventListener('click', getQuickLinks);
form.addEventListener("submit", (event) => {
event.preventDefault();
const data = [];
const key = form.elements["quick-link-key"].value;
const url = form.elements["quick-link-url"].value;
data.push(key);
data.push(url);
chrome.runtime.sendMessage({ type: 'form_submission', data }, (response) => {
console.log(response.message);
});
alert_success.style.display = 'block';
document.getElementById("user-input").reset();
storage.style.display = 'none';
storage.innerHTML = "";
getLinks.innerHTML = "View Links";
});

const getLinks = document.getElementById('get-links');
getLinks.addEventListener('click', getQuickLinks);

function getQuickLinks() {
if (getLinks.innerHTML == "Hide Links") {
getLinks.innerHTML = "View Links";
storage.style.display != 'none';
storage.innerHTML = "";
} else {
getLinks.innerHTML = "Hide Links";
chrome.storage.sync.get(['redirect_link'], (result) => {
if (storage.style.display != 'none') {
storage.innerHTML = "";
}
for (let key in result.redirect_link) {
const link = document.createElement('p');
const bold = document.createElement('span');
const breakLine = document.createElement('br');
bold.textContent = key;
link.textContent = result.redirect_link[key];
link.prepend(breakLine);
link.prepend(bold);
storage.appendChild(link);
}
storage.style.display = 'block';
cancelEditButton.addEventListener('click', () => {
editForm.style.display = 'none';
storage.style.display = 'block';
});


function getQuickLinks() {
if (getLinks.innerHTML == "Hide Links") {
getLinks.innerHTML = "View Links";
storage.style.display != 'none';
storage.innerHTML = "";

} else {
getLinks.innerHTML = "Hide Links";
chrome.storage.sync.get(['redirect_link'], (result) => {
if (storage.style.display != 'none') {
storage.innerHTML = "";
}
for (let key in result.redirect_link) {
const template = document.getElementById('link-template').content.cloneNode(true);
const linkItem = template.querySelector('.link-item');
template.querySelector('.link-key').textContent = key;
template.querySelector('.link-url').textContent = result.redirect_link[key];

const deleteButton = template.querySelector('.delete-button');
deleteButton.addEventListener('click', () => deleteLink(key));
deleteButton.addEventListener('mouseover', () => linkItem.classList.add('strike-through'));
deleteButton.addEventListener('mouseout', () => linkItem.classList.remove('strike-through'));

template.querySelector('.edit-button').addEventListener('click', () => showEditForm(key, result.redirect_link[key]));
storage.appendChild(template);
}
storage.style.display = 'block';
});
}
}

function showEditForm(key, url) {
storage.style.display = 'none';
editForm.style.display = 'block';
editForm.elements["edit-link-key"].value = key;
editForm.elements["edit-link-url"].value = url;
editButton.onclick = (event) => {
event.preventDefault();
editLink(key);
};
}

function editLink(oldKey) {
const newKey = editForm.elements["edit-link-key"].value;
const newUrl = editForm.elements["edit-link-url"].value;
if (newKey && newUrl) {
chrome.runtime.sendMessage({
type: 'edit_submission',
oldKey: oldKey,
newKey: newKey,
newUrl: newUrl
}, (response) => {
console.log(response.message);
editForm.style.display = 'none';
storage.style.display = 'block';
getQuickLinks();
});
}
}

function deleteLink(key) {
chrome.runtime.sendMessage({
type: 'delete_link',
key: key
}, (response) => {
console.log(response.message);
getQuickLinks();
});
}
}
});
6 changes: 0 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

A lightweight and simple link management extension for Chrome based browsers.



[<img alt="Chrome Web Store" width="200px" style="border-radius:5px;" src="https://github.com/mufasa159/quick-link/assets/40151627/bde675a7-9144-4b30-bac2-efcf855ce758"/>](https://chrome.google.com/webstore/detail/quick-link/napbhpnpfhkeaemdbclbncchalbialkc)

[Chrome Web Store](https://chrome.google.com/webstore/detail/quick-link/napbhpnpfhkeaemdbclbncchalbialkc)

### Usage

1. Install the extension
Expand Down
Loading

0 comments on commit 9004a03

Please sign in to comment.