Skip to content

Commit

Permalink
Merge pull request #7 from nomandhoni-cs/add-context-menu-post
Browse files Browse the repository at this point in the history
Implemented Context menu to directly post in Showwcase and Changed localstorage method
  • Loading branch information
nomandhoni-cs authored May 11, 2023
2 parents 34f3e39 + b22fe39 commit f6f4724
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 59 deletions.
64 changes: 64 additions & 0 deletions js/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
chrome.runtime.onInstalled.addListener(function() {
try {
chrome.contextMenus.create({
"title": "Post to Showwand",
"contexts": ["selection"],
"id": "showwandSelection"
}, function() {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError);
} else {
console.log("Context menu created successfully");
}
});
} catch (error) {
console.error(error);
}
});

chrome.contextMenus.onClicked.addListener(function(info, tab) {
try {
if (info.menuItemId === "showwandSelection") {
chrome.storage.local.get("showwand-api-key", function(data) {
var apiKey = data["showwand-api-key"];
console.log(apiKey);
if (!apiKey) {
// chrome.runtime.openOptionsPage();
return;
}
let selectedText = info.selectionText;
console.log("The selected text is: " + selectedText);
postFetchFunc(selectedText, apiKey, function() {
console.log("Successfully posted to Showwand");
});
});
}
} catch (error) {
console.error(error);
}
});


function postFetchFunc(postDescription, apiKey, callback) {
try {
fetch('https://cache.showwcase.com/threads', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': apiKey
},
body: JSON.stringify({
message: postDescription
})
})
.then(response => response.json())
.then(data => {
console.log(data);
callback();
})
.catch(error => console.error(error));
} catch (error) {
console.error(error);
}
}

31 changes: 18 additions & 13 deletions js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ getCurrentUrl()
const postBtn = document.getElementById('post-submit-btn');
const settingBtn = document.getElementById('setting-btn');
const apiKeyInput = document.getElementById('api-key-input');
const apiKeySavedMsg = document.getElementById('api-key-saved');
const apiKeyModal = document.getElementById('api-key-modal');
const saveApiKeyBtn = document.getElementById('save-api-key-btn');

Expand All @@ -40,17 +39,19 @@ getCurrentUrl()
}

function saveApiKey(apiKey) {
localStorage.setItem('showwand-api-key', apiKey);
apiKeySavedMsg.style.display = 'block';
setTimeout(() => {
apiKeySavedMsg.style.display = 'none';
}, 3000);
}

const savedApiKey = localStorage.getItem('showwand-api-key');
if (savedApiKey) {
apiKeyInput.value = savedApiKey;
}
chrome.storage.local.set({ "showwand-api-key": apiKey }, function() {
saveApiKeyBtn.innerText = 'Saved';
setTimeout(() => {
saveApiKeyBtn.innerText = 'Save';
}, 3000);
});
}

const savedApiKey = chrome.storage.local.get("showwand-api-key", function(data) {
if (data["showwand-api-key"]) {
apiKeyInput.value = data["showwand-api-key"];
}
});

postBtn.addEventListener('click', () => {
const postDescription = document.getElementById('post-description');
Expand All @@ -60,6 +61,10 @@ getCurrentUrl()
fullPostDescription = fullPostDescription + '\n ' + url;
const apiKey = apiKeyInput.value;
postFetchFunc(title, fullPostDescription, apiKey, () => {
if (!apiKey) {
alert('Please provide an Showwcase API key from Settings Option.');
return;
}
window.close();
});
});
Expand All @@ -72,7 +77,7 @@ getCurrentUrl()
const apiKey = apiKeyInput.value;
if (apiKey) {
saveApiKey(apiKey);
apiKeyModal.style.display = 'none';
console.log('API key saved.');
} else {
alert('Please provide an API key.');
}
Expand Down
65 changes: 31 additions & 34 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
{
"name": "Showwand",
"description": "A magical Browser extension, which directly post, delete in your Showwcase thread.",
"version": "1.0",
"manifest_version": 3,
"icons": {
"16": "assets/icon16.png",
"32": "assets/icon32.png",
"48": "assets/icon48.png",
"128": "assets/icon128.png"
},
"action": {
"default_popup": "popup.html"
},
"permissions": [
"activeTab"
],
"options_ui": {
"page": "options.html",
"open_in_tab": true
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["js/content.js"],
"run_at": "document_end"
}
],
"context_menus": {
"showwand": {
"title": "Post in Showwcase",
"contexts": ["selection"],
"onclick": "handleSelection"
}
"name": "Showwand",
"description": "A magical Browser extension, which directly post, delete in your Showwcase thread.",
"version": "0.5.0",
"manifest_version": 3,
"icons": {
"16": "assets/icon16.png",
"32": "assets/icon32.png",
"48": "assets/icon48.png",
"128": "assets/icon128.png"
},
"action": {
"default_popup": "popup.html"
},
"permissions": [
"activeTab",
"contextMenus",
"storage"
],
"options_ui": {
"page": "options.html",
"open_in_tab": true
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["js/content.js"],
"run_at": "document_end"
}
],
"background": {
"service_worker": "js/background.js"
}
}
12 changes: 0 additions & 12 deletions options.html
Original file line number Diff line number Diff line change
@@ -1,12 +0,0 @@
<!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>Options Page</title>
</head>
<body>

</body>
</html>

0 comments on commit f6f4724

Please sign in to comment.