Skip to content

Commit

Permalink
the pdf copy route was not reliably working on all pdf hosted sites -…
Browse files Browse the repository at this point in the history
… and was requiring extensive permissions - reworked to bring permissions down significantly and provide instructions for the user to copy and paste (Ctrl-c Ctrl-v) directly into the popup window to submit if the tab has no ID
  • Loading branch information
John Rogers committed Feb 1, 2025
1 parent f0c2053 commit 552f8fc
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 94 deletions.
Binary file added plugin/Screenshot 640.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 33 additions & 57 deletions plugin/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const createHarmonyUrl = ({ questions, instrument_name }) => {

// Create context menu item
chrome.runtime.onInstalled.addListener(() => {
// Create different menu items for PDFs and regular pages
chrome.contextMenus.create({
id: "sendToHarmony",
title: "Send to Harmony",
Expand All @@ -41,22 +42,9 @@ chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.set({ history: [] });
});

// Function to find or create Harmony tab
async function findOrCreateHarmonyTab(url) {
// First, try to find an existing tab with our target name in the URL
const tabs = await chrome.tabs.query({});
const harmonyTab = tabs.find(
(tab) => tab.url && tab.url.includes(harmonyURL)
);

if (harmonyTab) {
// Update existing tab
await chrome.tabs.update(harmonyTab.id, { url: url, active: true });
await chrome.windows.update(harmonyTab.windowId, { focused: true });
} else {
// Create new tab
await chrome.tabs.create({ url: url });
}
// Function to open Harmony URL in new tab
function openHarmonyTab(url) {
chrome.tabs.create({ url: url });
}

// Listen for messages from popup
Expand All @@ -65,52 +53,40 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
findOrCreateHarmonyTab(request.url);
return true;
}
if (request.action === "returnCopied") {
findOrCreateHarmonyTab(request.url);
if (request.action === "processPdfText") {
processSelection(request.text, request.tab);
return true;
}
});

chrome.contextMenus.onClicked.addListener(function (info, tab) {
chrome.contextMenus.onClicked.addListener(async function (info, tab) {
if (info.menuItemId === "sendToHarmony") {
if (tab?.id > -1) {
chrome.scripting
.executeScript({
target: { tabId: tab.id },
function: () => {
const selection = document.getSelection();
return selection ? selection.toString() : "";
},
})
.then((resultArray) => {
const result = resultArray[0];
const selectedText =
result && result && result.result ? result.result : ""; // Handle various result possibilities
processSelection(selectedText, tab);
})
.catch((error) => {
console.error("Error getting selected text:", error);
});
} else {
// If tab.id is null (eg in a PDF), trigger the copy command and then read from clipboard
chrome.tabs
.sendMessage({
action: "copySelection",
})
.then((response) => {
if (response?.success) {
try {
navigator.clipboard
.readText()
.then((selectedText) => processSelection(selectedText, tab));
} catch (err) {
console.error("Failed to read clipboard contents: ", err);
}
}
})
.catch((error) => {
console.error("Error executing copy command:", error);
});
if (tab?.id === -1 || tab?.url?.toLowerCase().includes("pdf")) {
// For PDF tabs, show popup
chrome.action.openPopup();
return;
}

// For non-PDF tabs, use scripting API
try {
const resultArray = await chrome.scripting.executeScript({
target: { tabId: tab.id },
function: () => {
const selection = document.getSelection();
return selection ? selection.toString() : "";
},
});
const selectedText = resultArray[0]?.result || "";
if (selectedText) {
processSelection(selectedText, tab);
}
} catch (error) {
console.error("Error getting selected text:", error);
chrome.action.setBadgeText({ text: "!" });
chrome.action.setBadgeBackgroundColor({ color: "#F44336" });
setTimeout(() => {
chrome.action.setBadgeText({ text: "" });
}, 2000);
}
}
});
Expand Down
20 changes: 0 additions & 20 deletions plugin/content.js

This file was deleted.

20 changes: 3 additions & 17 deletions plugin/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,8 @@
"manifest_version": 3,
"name": "Send to Harmony",
"version": "1.0",
"description": "Send selected text to Harmony with a right-click",
"permissions": [
"contextMenus",
"storage",
"scripting",
"activeTab",
"tabs",
"clipboardRead"
],
"description": "Send selected text to Harmony with a right-click. For PDFs, use the popup to paste your selected text.",
"permissions": ["contextMenus", "storage", "scripting", "activeTab", "tabs"],
"icons": {
"16": "icons/16.png",
"48": "icons/48.png",
Expand All @@ -34,12 +27,5 @@
}
]
},
"host_permissions": ["*://*/*.pdf", "*://*/*pdf*"],
"content_scripts": [
{
"matches": ["*://*/*.pdf", "*://*/*pdf*"],
"js": ["content.js"],
"all_frames": true
}
]
"host_permissions": []
}
34 changes: 34 additions & 0 deletions plugin/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,40 @@ <h2>Send to Harmony</h2>
other websites to add further items to your harmonisation.
</p>

<div id="pdfInput" style="display: none; margin: 20px 0">
<p style="color: #666">This appears to be a PDF. Please:</p>
<ol style="text-align: left; color: #666">
<li>Select the text you want</li>
<li>Press Ctrl+C to copy</li>
<li>Paste (Ctrl+V) below:</li>
</ol>
<textarea
id="pdfText"
style="
width: 100%;
height: 100px;
margin: 10px 0;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
"
placeholder="Paste your selected text here..."
></textarea>
<button
id="submitPdf"
style="
background: #007bff;
color: white;
border: none;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
"
>
Send to Harmony
</button>
</div>

<div class="history">
<h3>Recent Imports</h3>
<div id="historyList">
Expand Down
24 changes: 24 additions & 0 deletions plugin/popup.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
// Check if we're dealing with a PDF tab
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const currentTab = tabs[0];
if (currentTab?.id === -1 || currentTab?.url?.toLowerCase().includes("pdf")) {
document.getElementById("pdfInput").style.display = "block";
}
});

// Handle PDF text submission
document.getElementById("submitPdf").addEventListener("click", function () {
const text = document.getElementById("pdfText").value;
if (text) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const currentTab = tabs[0];
chrome.runtime.sendMessage({
action: "processPdfText",
text: text,
tab: currentTab,
});
window.close();
});
}
});

// Function to format relative time
function getRelativeTime(timestamp) {
const now = new Date();
Expand Down

0 comments on commit 552f8fc

Please sign in to comment.