Skip to content

Commit

Permalink
Merge pull request #2 from leknoppix/url_perso
Browse files Browse the repository at this point in the history
Donner la possibilité à l'utilisateur de charger sa propre liste.
  • Loading branch information
yoanbernabeu committed Oct 30, 2023
2 parents be8d295 + 3b2ecf1 commit 41211a1
Show file tree
Hide file tree
Showing 11 changed files with 276 additions and 91 deletions.
Binary file added chrome/icons/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 13 additions & 5 deletions chrome/manifest.json
@@ -1,19 +1,27 @@
{
"manifest_version": 3,
"name": "YoanDevGPT",
"version": "0.1.3",
"version": "0.1.4",
"description": "Collection de prompts pour spécialiser les intéractions avec ChatGPT",
"action": {
"default_icon": {
"16": "icons/icon16.png",
"32": "icons/icon32.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
},
"default_title": "YoanDevGPT",
"default_popup": "src/popup.html"
},
"icons": {
"16": "icons/icon16.png",
"32": "icons/icon32.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
},
"action": {
"default_popup": "src/popup.html"
},
"permissions": ["activeTab", "contextMenus", "scripting"],
"permissions": ["storage", "activeTab", "contextMenus", "scripting"],
"host_permissions": ["https://raw.githubusercontent.com/"],
"options_page": "src/popup.html",
"background": {
"service_worker": "src/background.js"
},
Expand Down
97 changes: 67 additions & 30 deletions chrome/src/background.js
@@ -1,41 +1,78 @@
const DATA_URL = "https://raw.githubusercontent.com/yoanbernabeu/YoanDevGPT/main/data.json";

chrome.runtime.onInstalled.addListener(() => {
fetch(DATA_URL)
.then((response) => response.json())
.then((data) => {
data.forEach((item, index) => {
chrome.contextMenus.create({
id: index.toString(),
title: item.title,
contexts: ["editable"],
parentId: "parent",
const DEFAULT_DATA_URL = "https://raw.githubusercontent.com/yoanbernabeu/YoanDevGPT/main/data.json";

chrome.runtime.onInstalled.addListener(setup);
chrome.runtime.onStartup.addListener(setup);

function setup() {
// clear all context menus entries firstly
chrome.contextMenus.removeAll(function() {
chrome.storage.local.get('dataURL', data => {
let DATA_URL = data.dataURL || DEFAULT_DATA_URL;

fetch(DATA_URL)
.then((response) => response.json())
.then((data) => {
data.forEach((item, index) => {
chrome.contextMenus.create({
id: index.toString(),
title: item.title,
contexts: ["editable"],
parentId: "parent",
});
});
});

chrome.contextMenus.create({
id: "parent",
title: "YoanDevGPT",
contexts: ["editable"],
});
});

chrome.contextMenus.create({
id: "parent",
title: "YoanDevGPT",
contexts: ["editable"],
// Create a context menu to reset URL to default
chrome.contextMenus.create({
id: "reset",
title: "Reset URL to default",
contexts: ["browser_action"],
});
});
});
});
}

chrome.contextMenus.onClicked.addListener((info, tab) => {
const index = parseInt(info.menuItemId);

fetch(DATA_URL)
.then((response) => response.json())
.then((data) => {
const prompt = data[index].prompt;
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: setInputField,
args: [prompt],
});
});
chrome.storage.local.get('dataURL', data => {
let DATA_URL = data.dataURL || DEFAULT_DATA_URL;

console.log(`URL de données actuelle : ${DATA_URL}`);

if (info.menuItemId === "reset") {
DATA_URL = DEFAULT_DATA_URL;
chrome.storage.local.set({ dataURL: DATA_URL });
} else {
const index = parseInt(info.menuItemId);

fetch(DATA_URL)
.then((response) => response.json())
.then((data) => {
const prompt = data[index].prompt;
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: setInputField,
args: [prompt],
});
});
}
});
});

function setInputField(prompt) {
document.activeElement.value = prompt;
}

chrome.storage.onChanged.addListener(function(changes) {
for (let key in changes) {
if (key === 'dataURL') {
setup();
break;
}
}
});
32 changes: 21 additions & 11 deletions chrome/src/popup.html
@@ -1,23 +1,33 @@
<!doctype html>
<html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>YoanDevGPT</title>
<style>
body {
margin: 0;
width:700px;
height: auto;
margin: 10px;
font-family: "Arial, sans-serif";
background-color: #fff;
center: 0;
}
</style>
</head>
<body>
<script src="popup.js"></script>
<div style="text-align:center;">
<img src="../logo.png" width="100" height="100">
<h1>YoanDevGPT</h1>
<p>Collection de prompts pour ChatGPT</p>
<p>Pour l'utiliser, faites un clic-droit dans un le champ de saisi de ChatGPT et cliquez sur "YoanDevGPT"</p>
</div>
<script src="popup.js"></script>
<div style="text-align:center;">
<img src="../icons/logo.png" width="100" height="100">
<h1>YoanDevGPT</h1>
<p>Collection de prompts pour ChatGPT</p>
<p>Pour l'utiliser, faites un clic-droit dans un le champ de saisi de ChatGPT et cliquez sur "YoanDevGPT"</p>
<h1>Options de YoanDevGPT</h1>
<p id="message"></p>
<form id="options-form">
<label for="data-url">URL de données :</label>
<input type="text" id="data-url" name="data-url">
<button type="submit">Sauvegarder</button>
<button type="button" id="reset-btn">Réinitialiser</button>
</form>
</div>
</body>
</html>
</html>
39 changes: 39 additions & 0 deletions chrome/src/popup.js
@@ -0,0 +1,39 @@
document.addEventListener('DOMContentLoaded', function () {
const dataUrlElement = document.getElementById('data-url');
const optionsForm = document.getElementById('options-form');
const resetBtn = document.getElementById('reset-btn');
const messageElement = document.getElementById('message');

// Load saved data url
chrome.storage.local.get(['dataURL'], function (result) {
if (result.dataURL) {
dataUrlElement.value = result.dataURL;
}
});

optionsForm.addEventListener('submit', function (e) {
e.preventDefault();
// Save data url
chrome.storage.local.set({ dataURL: dataUrlElement.value }, function () {
messageElement.textContent = 'URL de données sauvegardée.';
});
setTimeout(function () {
messageElement.textContent = '';
window.close();
}, 4000);
});

resetBtn.addEventListener('click', function () {
// Remove the stored data url
chrome.storage.local.remove('dataURL', function () {
console.log('Data url cleared.');
dataUrlElement.value = '';
messageElement.textContent = "URL de données réinitialisée.";
});
// Clear the message and close the popup after 4 seconds
setTimeout(function () {
messageElement.textContent = '';
window.close();
}, 4000);
});
});
Binary file added firefox/icons/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 7 additions & 3 deletions firefox/manifest.json
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "YoanDevGPT",
"version": "0.1.3",
"version": "0.1.4",
"description": "Collection de prompts pour spécialiser les interactions avec ChatGPT",
"icons": {
"16": "icons/icon16.png",
Expand All @@ -18,7 +18,11 @@
"128": "icons/icon128.png"
}
},
"permissions": ["activeTab",
"options_ui": {
"page": "src/popup.html"
},
"permissions": ["storage",
"activeTab",
"contextMenus",
"<all_urls>"],
"background": {
Expand All @@ -30,4 +34,4 @@
"js": ["src/contentScript.js"]
}
]
}
}
96 changes: 67 additions & 29 deletions firefox/src/background.js
@@ -1,37 +1,75 @@
const DATA_URL = "https://raw.githubusercontent.com/yoanbernabeu/YoanDevGPT/main/data.json";

browser.runtime.onInstalled.addListener(() => {
fetch(DATA_URL)
.then((response) => response.json())
.then((data) => {
data.forEach((item, index) => {
browser.contextMenus.create({
id: index.toString(),
title: item.title,
contexts: ["editable"],
const DEFAULT_DATA_URL = "https://raw.githubusercontent.com/yoanbernabeu/YoanDevGPT/main/data.json";

browser.runtime.onInstalled.addListener(setup);
browser.runtime.onStartup.addListener(setup);

function setup() {
// clear all context menus entries firstly
browser.contextMenus.removeAll().then(() => {
browser.storage.local.get('dataURL').then(data => {
let DATA_URL = data.dataURL || DEFAULT_DATA_URL;

fetch(DATA_URL)
.then((response) => response.json())
.then((data) => {
data.forEach((item, index) => {
browser.contextMenus.create({
id: index.toString(),
title: item.title,
contexts: ["editable"],
parentId: "parent",
});
});
});

browser.contextMenus.create({
id: "parent",
title: "YoanDevGPT",
contexts: ["editable"],
});

// Create a context menu to reset URL to default
browser.contextMenus.create({
id: "reset",
title: "Reset URL to default",
contexts: ["browser_action"],
});
});
});
}

browser.contextMenus.create({
id: "parent",
title: "YoanDevGPT",
contexts: ["editable"],
browser.contextMenus.onClicked.addListener((info, tab) => {
browser.storage.local.get('dataURL').then(data => {
let DATA_URL = data.dataURL || DEFAULT_DATA_URL;

console.log(`URL de données actuelle : ${DATA_URL}`);

if (info.menuItemId === "reset") {
DATA_URL = DEFAULT_DATA_URL;
browser.storage.local.set({ dataURL: DATA_URL });
} else {
const index = parseInt(info.menuItemId);

fetch(DATA_URL)
.then((response) => response.json())
.then((data) => {
const prompt = data[index].prompt;
browser.tabs.executeScript(
tab.id,
{
code: `document.activeElement.value = "${prompt}";`
}
);
});
}
});
});

browser.contextMenus.onClicked.addListener((info, tab) => {
const index = parseInt(info.menuItemId);

fetch(DATA_URL)
.then((response) => response.json())
.then((data) => {
const prompt = data[index].prompt;
browser.tabs.executeScript(
tab.id,
{
code: `document.activeElement.value = "${prompt}";`
}
);
});
browser.storage.onChanged.addListener(function(changes) {
for (let key in changes) {
if (key === 'dataURL') {
setup();
break;
}
}
});

0 comments on commit 41211a1

Please sign in to comment.