Skip to content

Commit 552f8fc

Browse files
author
John Rogers
committed
the pdf copy route was not reliably working on all pdf hosted sites - 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
1 parent f0c2053 commit 552f8fc

6 files changed

Lines changed: 94 additions & 94 deletions

File tree

plugin/Screenshot 640.png

115 KB
Loading

plugin/background.js

Lines changed: 33 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const createHarmonyUrl = ({ questions, instrument_name }) => {
3232

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

44-
// Function to find or create Harmony tab
45-
async function findOrCreateHarmonyTab(url) {
46-
// First, try to find an existing tab with our target name in the URL
47-
const tabs = await chrome.tabs.query({});
48-
const harmonyTab = tabs.find(
49-
(tab) => tab.url && tab.url.includes(harmonyURL)
50-
);
51-
52-
if (harmonyTab) {
53-
// Update existing tab
54-
await chrome.tabs.update(harmonyTab.id, { url: url, active: true });
55-
await chrome.windows.update(harmonyTab.windowId, { focused: true });
56-
} else {
57-
// Create new tab
58-
await chrome.tabs.create({ url: url });
59-
}
45+
// Function to open Harmony URL in new tab
46+
function openHarmonyTab(url) {
47+
chrome.tabs.create({ url: url });
6048
}
6149

6250
// Listen for messages from popup
@@ -65,52 +53,40 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
6553
findOrCreateHarmonyTab(request.url);
6654
return true;
6755
}
68-
if (request.action === "returnCopied") {
69-
findOrCreateHarmonyTab(request.url);
56+
if (request.action === "processPdfText") {
57+
processSelection(request.text, request.tab);
7058
return true;
7159
}
7260
});
7361

74-
chrome.contextMenus.onClicked.addListener(function (info, tab) {
62+
chrome.contextMenus.onClicked.addListener(async function (info, tab) {
7563
if (info.menuItemId === "sendToHarmony") {
76-
if (tab?.id > -1) {
77-
chrome.scripting
78-
.executeScript({
79-
target: { tabId: tab.id },
80-
function: () => {
81-
const selection = document.getSelection();
82-
return selection ? selection.toString() : "";
83-
},
84-
})
85-
.then((resultArray) => {
86-
const result = resultArray[0];
87-
const selectedText =
88-
result && result && result.result ? result.result : ""; // Handle various result possibilities
89-
processSelection(selectedText, tab);
90-
})
91-
.catch((error) => {
92-
console.error("Error getting selected text:", error);
93-
});
94-
} else {
95-
// If tab.id is null (eg in a PDF), trigger the copy command and then read from clipboard
96-
chrome.tabs
97-
.sendMessage({
98-
action: "copySelection",
99-
})
100-
.then((response) => {
101-
if (response?.success) {
102-
try {
103-
navigator.clipboard
104-
.readText()
105-
.then((selectedText) => processSelection(selectedText, tab));
106-
} catch (err) {
107-
console.error("Failed to read clipboard contents: ", err);
108-
}
109-
}
110-
})
111-
.catch((error) => {
112-
console.error("Error executing copy command:", error);
113-
});
64+
if (tab?.id === -1 || tab?.url?.toLowerCase().includes("pdf")) {
65+
// For PDF tabs, show popup
66+
chrome.action.openPopup();
67+
return;
68+
}
69+
70+
// For non-PDF tabs, use scripting API
71+
try {
72+
const resultArray = await chrome.scripting.executeScript({
73+
target: { tabId: tab.id },
74+
function: () => {
75+
const selection = document.getSelection();
76+
return selection ? selection.toString() : "";
77+
},
78+
});
79+
const selectedText = resultArray[0]?.result || "";
80+
if (selectedText) {
81+
processSelection(selectedText, tab);
82+
}
83+
} catch (error) {
84+
console.error("Error getting selected text:", error);
85+
chrome.action.setBadgeText({ text: "!" });
86+
chrome.action.setBadgeBackgroundColor({ color: "#F44336" });
87+
setTimeout(() => {
88+
chrome.action.setBadgeText({ text: "" });
89+
}, 2000);
11490
}
11591
}
11692
});

plugin/content.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

plugin/manifest.json

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,8 @@
22
"manifest_version": 3,
33
"name": "Send to Harmony",
44
"version": "1.0",
5-
"description": "Send selected text to Harmony with a right-click",
6-
"permissions": [
7-
"contextMenus",
8-
"storage",
9-
"scripting",
10-
"activeTab",
11-
"tabs",
12-
"clipboardRead"
13-
],
5+
"description": "Send selected text to Harmony with a right-click. For PDFs, use the popup to paste your selected text.",
6+
"permissions": ["contextMenus", "storage", "scripting", "activeTab", "tabs"],
147
"icons": {
158
"16": "icons/16.png",
169
"48": "icons/48.png",
@@ -34,12 +27,5 @@
3427
}
3528
]
3629
},
37-
"host_permissions": ["*://*/*.pdf", "*://*/*pdf*"],
38-
"content_scripts": [
39-
{
40-
"matches": ["*://*/*.pdf", "*://*/*pdf*"],
41-
"js": ["content.js"],
42-
"all_frames": true
43-
}
44-
]
30+
"host_permissions": []
4531
}

plugin/popup.html

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,40 @@ <h2>Send to Harmony</h2>
6363
other websites to add further items to your harmonisation.
6464
</p>
6565

66+
<div id="pdfInput" style="display: none; margin: 20px 0">
67+
<p style="color: #666">This appears to be a PDF. Please:</p>
68+
<ol style="text-align: left; color: #666">
69+
<li>Select the text you want</li>
70+
<li>Press Ctrl+C to copy</li>
71+
<li>Paste (Ctrl+V) below:</li>
72+
</ol>
73+
<textarea
74+
id="pdfText"
75+
style="
76+
width: 100%;
77+
height: 100px;
78+
margin: 10px 0;
79+
padding: 8px;
80+
border: 1px solid #ddd;
81+
border-radius: 4px;
82+
"
83+
placeholder="Paste your selected text here..."
84+
></textarea>
85+
<button
86+
id="submitPdf"
87+
style="
88+
background: #007bff;
89+
color: white;
90+
border: none;
91+
padding: 8px 16px;
92+
border-radius: 4px;
93+
cursor: pointer;
94+
"
95+
>
96+
Send to Harmony
97+
</button>
98+
</div>
99+
66100
<div class="history">
67101
<h3>Recent Imports</h3>
68102
<div id="historyList">

plugin/popup.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
// Check if we're dealing with a PDF tab
2+
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
3+
const currentTab = tabs[0];
4+
if (currentTab?.id === -1 || currentTab?.url?.toLowerCase().includes("pdf")) {
5+
document.getElementById("pdfInput").style.display = "block";
6+
}
7+
});
8+
9+
// Handle PDF text submission
10+
document.getElementById("submitPdf").addEventListener("click", function () {
11+
const text = document.getElementById("pdfText").value;
12+
if (text) {
13+
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
14+
const currentTab = tabs[0];
15+
chrome.runtime.sendMessage({
16+
action: "processPdfText",
17+
text: text,
18+
tab: currentTab,
19+
});
20+
window.close();
21+
});
22+
}
23+
});
24+
125
// Function to format relative time
226
function getRelativeTime(timestamp) {
327
const now = new Date();

0 commit comments

Comments
 (0)