Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added quick generate options in Extension context menu #80

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 87 additions & 70 deletions extension/public/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,88 @@ chrome.runtime.onInstalled.addListener(() => {
title: "Generate Quiz with Selected Text",
contexts: ["selection"]
});
});

// Handle context menu clicks
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
chrome.contextMenus.create({
id: "get_mcq",
title: "Generate MCQ questions",
contexts: ["selection"]
});
chrome.contextMenus.create({
id: "get_boolq",
title: "Generate true/ false type questions",
contexts: ["selection"]
});
chrome.contextMenus.create({
id: "get_shortq",
title: "Generate Short questions",
contexts: ["selection"]
});
});

async function openExtensionWithUrl(url, tab) {
try {
// Set popup
chrome.action.setPopup({
popup: url
});

// Open popup
chrome.action.openPopup();

// This exists to ensure user is notified of change in case the popup did not open
// Show a badge to indicate text was captured
chrome.action.setBadgeText({
text: "!"
});
chrome.action.setBadgeBackgroundColor({
color: "#FF005C"
});

// Clear the badge after 2 seconds and reset popup view
setTimeout(async () => {
chrome.action.setBadgeText({ text: "" });

await chrome.action.setPopup({
popup: 'src/pages/home/home.html'
});
}, 2000);

} catch (error) {
throw error;
}
}

// Handle context menu clicks
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
try {
// Store the selected text locally
await chrome.storage.local.set({ selectedText: info.selectionText });

if (info.menuItemId === "askExtension" && info.selectionText) {
try {
// Store the selected text first
await chrome.storage.local.set({
selectedText: info.selectionText
});

// Inject content script if needed
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["contentScript.js"]
});
// Option page to select question type and difficulty
await openExtensionWithUrl("src/pages/home/home.html", tab);
} else if (
info.menuItemId === "get_mcq" ||
info.menuItemId === "get_shortq" ||
info.menuItemId === "get_boolq"
) {

// Send message to content script
await chrome.tabs.sendMessage(tab.id, {
selectedText: info.selectionText
});

// Open the popup
// Note: Chrome extensions can't programmatically open the popup,
// but we can show the user where to click
chrome.action.setPopup({
popup: "src/popup/popup.html"
});

// Show a badge to indicate text was captured
chrome.action.setBadgeText({
text: "!"
});
chrome.action.setBadgeBackgroundColor({
color: "#FF005C"
});

// Clear the badge after 2 seconds
setTimeout(() => {
chrome.action.setBadgeText({ text: "" });
}, 2000);

} catch (error) {
console.error("Error in context menu handler:", error);
}
}
});

// Listen for messages from content script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.type === "TEXT_SELECTED") {
chrome.storage.local.set({
selectedText: request.text
}, () => {
console.log("Text saved to storage:", request.text);
sendResponse({ status: "success" });
});
return true; // Required for async sendResponse
// Construct the URL with the question type
const baseUrl = "src/pages/text_input/text_input.html";
const encodedUrl = `${baseUrl}?questionType=${encodeURIComponent(info.menuItemId)}`;

// Open the extension with the constructed URL
await openExtensionWithUrl(encodedUrl, tab);
}
});

//Clear badge when popup is opened
chrome.action.onClicked.addListener(() => {
chrome.action.setBadgeText({ text: "" });
});
} catch (error) {
console.error("Error in handling context menu:", error);
}
});

//Clear badge when popup is opened
chrome.action.onClicked.addListener(() => {
chrome.action.setBadgeText({ text: "" });
});


chrome.storage.onChanged.addListener((changes, namespace) => {
for (let [key, { oldValue, newValue }] of Object.entries(changes)) {
Expand All @@ -90,11 +107,11 @@ chrome.storage.onChanged.addListener((changes, namespace) => {
});


// Optional: Handle extension install/update
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === "install") {
console.log("Extension installed");
} else if (details.reason === "update") {
console.log("Extension updated");
}
});
// Optional: Handle extension install/update
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === "install") {
console.log("Extension installed");
} else if (details.reason === "update") {
console.log("Extension updated");
}
});
13 changes: 0 additions & 13 deletions extension/public/contentScript.js

This file was deleted.

6 changes: 0 additions & 6 deletions extension/public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,5 @@
"resources": ["static/js/*", "static/css/*", "src/assets/*"],
"matches": ["<all_urls>"]
}
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["contentScript.js"]
}
]
}
20 changes: 20 additions & 0 deletions extension/src/pages/text_input/TextInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ function Second() {
});
}, [])

// This fires when text is changed and if url params are present it indicates
// that this page was loaded by context menu
useEffect(() => {
if (text) {
console.log("Updated Text State: ", text);
checkUrlParams();
}
}, [text]);

const checkUrlParams = async () => {
const urlParams = new URLSearchParams(window.location.search);
const questionType = urlParams.get("questionType");

if(questionType) {
console.log("Question Type:", questionType);
localStorage.setItem('selectedQuestionType', questionType);
console.log(text)
handleSaveToLocalStorage();
}
}

const toggleSwitch = () => {
setIsToggleOn((isToggleOn + 1) % 2);
Expand Down