feat(content): intercept magnet links and display capture confirmation modal
- Capture click events on magnet: protocol links on all pages - Prevent default OS protocol handler interception - Render isolated Shadow DOM dialog displaying captured torrent name and hash - Provide Continue button to redirect to pairing page and dismiss controls
This commit is contained in:
1 parent
0ab1db04b1
commit
d2bd0895f8
2 files changed
+351
No files matched your search
@@ -0,0 +1,5 @@
|
||||
/* content.css for Magnetflix */
|
||||
#magnetflix-modal-host {
|
||||
position: relative;
|
||||
z-index: 2147483647;
|
||||
}
|
||||
+346
@@ -0,0 +1,346 @@
|
||||
// Content script for Magnetflix: intercepts magnet links and displays popup modal
|
||||
(function () {
|
||||
const browserAPI = window.browser || window.chrome;
|
||||
let activeModalHost = null;
|
||||
|
||||
// Helper to extract display name from magnet link
|
||||
function getMagnetDisplayName(magnetUrl) {
|
||||
try {
|
||||
const match = magnetUrl.match(/[?&]dn=([^&]+)/);
|
||||
if (match && match[1]) {
|
||||
return decodeURIComponent(match[1].replace(/\+/g, " "));
|
||||
}
|
||||
} catch (e) {}
|
||||
return "Torrent Magnet";
|
||||
}
|
||||
|
||||
// Helper to extract xt hash
|
||||
function getMagnetHash(magnetUrl) {
|
||||
try {
|
||||
const match = magnetUrl.match(/xt=urn:[a-z0-9]+:([a-zA-Z0-9]+)/i);
|
||||
if (match && match[1]) {
|
||||
return match[1];
|
||||
}
|
||||
} catch (e) {}
|
||||
return null;
|
||||
}
|
||||
|
||||
function showMagnetModal(magnetUrl) {
|
||||
// Remove existing modal if already open
|
||||
if (activeModalHost && document.body.contains(activeModalHost)) {
|
||||
activeModalHost.remove();
|
||||
}
|
||||
|
||||
const displayName = getMagnetDisplayName(magnetUrl);
|
||||
const hash = getMagnetHash(magnetUrl);
|
||||
const truncatedHash = hash ? `${hash.slice(0, 8)}...${hash.slice(-8)}` : "Valid link";
|
||||
|
||||
// Create host element with Shadow DOM for CSS isolation
|
||||
const host = document.createElement("div");
|
||||
host.id = "magnetflix-modal-host";
|
||||
const shadow = host.attachShadow({ mode: "open" });
|
||||
|
||||
// CSS styling inside Shadow DOM
|
||||
const style = document.createElement("style");
|
||||
style.textContent = `
|
||||
:host {
|
||||
all: initial;
|
||||
z-index: 2147483647;
|
||||
}
|
||||
.mf-backdrop {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
background: rgba(0, 0, 0, 0.7);
|
||||
backdrop-filter: blur(4px);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 2147483647;
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
||||
animation: mf-fade-in 0.15s ease-out;
|
||||
}
|
||||
@keyframes mf-fade-in {
|
||||
from { opacity: 0; }
|
||||
to { opacity: 1; }
|
||||
}
|
||||
.mf-modal {
|
||||
background: #18181b;
|
||||
color: #f4f4f5;
|
||||
border: 1px solid #3f3f46;
|
||||
border-radius: 14px;
|
||||
width: 440px;
|
||||
max-width: 90vw;
|
||||
padding: 24px;
|
||||
box-shadow: 0 20px 40px rgba(0, 0, 0, 0.6);
|
||||
box-sizing: border-box;
|
||||
text-align: center;
|
||||
animation: mf-slide-up 0.2s cubic-bezier(0.16, 1, 0.3, 1);
|
||||
}
|
||||
@keyframes mf-slide-up {
|
||||
from { transform: translateY(12px) scale(0.97); opacity: 0; }
|
||||
to { transform: translateY(0) scale(1); opacity: 1; }
|
||||
}
|
||||
.mf-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
.mf-logo-group {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
.mf-badge {
|
||||
font-size: 20px;
|
||||
}
|
||||
.mf-app-title {
|
||||
font-size: 15px;
|
||||
font-weight: 700;
|
||||
color: #e50914;
|
||||
letter-spacing: 0.5px;
|
||||
text-transform: uppercase;
|
||||
}
|
||||
.mf-close-btn {
|
||||
background: transparent;
|
||||
border: none;
|
||||
color: #a1a1aa;
|
||||
font-size: 18px;
|
||||
cursor: pointer;
|
||||
padding: 4px 8px;
|
||||
border-radius: 6px;
|
||||
transition: background 0.15s, color 0.15s;
|
||||
line-height: 1;
|
||||
}
|
||||
.mf-close-btn:hover {
|
||||
background: #27272a;
|
||||
color: #ffffff;
|
||||
}
|
||||
.mf-title {
|
||||
font-size: 20px;
|
||||
font-weight: 600;
|
||||
margin: 0 0 8px 0;
|
||||
color: #ffffff;
|
||||
}
|
||||
.mf-subtitle {
|
||||
font-size: 13px;
|
||||
color: #a1a1aa;
|
||||
margin: 0 0 16px 0;
|
||||
}
|
||||
.mf-info-box {
|
||||
background: #27272a;
|
||||
border: 1px solid #3f3f46;
|
||||
border-radius: 8px;
|
||||
padding: 12px;
|
||||
margin-bottom: 20px;
|
||||
text-align: left;
|
||||
}
|
||||
.mf-info-label {
|
||||
font-size: 11px;
|
||||
text-transform: uppercase;
|
||||
color: #71717a;
|
||||
font-weight: 600;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
.mf-info-name {
|
||||
font-size: 13px;
|
||||
font-weight: 500;
|
||||
color: #e4e4e7;
|
||||
word-break: break-all;
|
||||
line-height: 1.4;
|
||||
}
|
||||
.mf-info-hash {
|
||||
font-size: 11px;
|
||||
color: #a1a1aa;
|
||||
font-family: monospace;
|
||||
margin-top: 4px;
|
||||
}
|
||||
.mf-btn-primary {
|
||||
background: #e50914;
|
||||
color: #ffffff;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
padding: 12px 24px;
|
||||
font-size: 15px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
width: 100%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 8px;
|
||||
transition: background 0.15s, transform 0.1s;
|
||||
box-shadow: 0 4px 14px rgba(229, 9, 20, 0.4);
|
||||
}
|
||||
.mf-btn-primary:hover {
|
||||
background: #f6121d;
|
||||
transform: translateY(-1px);
|
||||
}
|
||||
.mf-btn-primary:active {
|
||||
transform: translateY(0);
|
||||
}
|
||||
`;
|
||||
|
||||
const container = document.createElement("div");
|
||||
container.className = "mf-backdrop";
|
||||
|
||||
const modal = document.createElement("div");
|
||||
modal.className = "mf-modal";
|
||||
|
||||
// Header
|
||||
const header = document.createElement("div");
|
||||
header.className = "mf-header";
|
||||
|
||||
const logoGroup = document.createElement("div");
|
||||
logoGroup.className = "mf-logo-group";
|
||||
const badge = document.createElement("span");
|
||||
badge.className = "mf-badge";
|
||||
badge.textContent = "🧲";
|
||||
const appTitle = document.createElement("span");
|
||||
appTitle.className = "mf-app-title";
|
||||
appTitle.textContent = "Magnetflix";
|
||||
logoGroup.appendChild(badge);
|
||||
logoGroup.appendChild(appTitle);
|
||||
|
||||
const closeBtn = document.createElement("button");
|
||||
closeBtn.className = "mf-close-btn";
|
||||
closeBtn.id = "mf-close";
|
||||
closeBtn.title = "Dismiss";
|
||||
closeBtn.textContent = "✕";
|
||||
|
||||
header.appendChild(logoGroup);
|
||||
header.appendChild(closeBtn);
|
||||
|
||||
// Title & subtitle
|
||||
const titleEl = document.createElement("h2");
|
||||
titleEl.className = "mf-title";
|
||||
titleEl.textContent = "Magnet link captured!";
|
||||
|
||||
const subtitleEl = document.createElement("p");
|
||||
subtitleEl.className = "mf-subtitle";
|
||||
subtitleEl.textContent = "Ready to pair with IMDb and send to FastAPI";
|
||||
|
||||
// Info box
|
||||
const infoBox = document.createElement("div");
|
||||
infoBox.className = "mf-info-box";
|
||||
|
||||
const infoLabel = document.createElement("div");
|
||||
infoLabel.className = "mf-info-label";
|
||||
infoLabel.textContent = "Captured Item";
|
||||
|
||||
const infoName = document.createElement("div");
|
||||
infoName.className = "mf-info-name";
|
||||
infoName.textContent = displayName;
|
||||
|
||||
const infoHash = document.createElement("div");
|
||||
infoHash.className = "mf-info-hash";
|
||||
infoHash.textContent = `Hash: ${truncatedHash}`;
|
||||
|
||||
infoBox.appendChild(infoLabel);
|
||||
infoBox.appendChild(infoName);
|
||||
infoBox.appendChild(infoHash);
|
||||
|
||||
// Continue button
|
||||
const continueBtn = document.createElement("button");
|
||||
continueBtn.className = "mf-btn-primary";
|
||||
continueBtn.id = "mf-continue";
|
||||
|
||||
const continueText = document.createElement("span");
|
||||
continueText.textContent = "Continue";
|
||||
const arrow = document.createElement("span");
|
||||
arrow.textContent = "→";
|
||||
|
||||
continueBtn.appendChild(continueText);
|
||||
continueBtn.appendChild(arrow);
|
||||
|
||||
modal.appendChild(header);
|
||||
modal.appendChild(titleEl);
|
||||
modal.appendChild(subtitleEl);
|
||||
modal.appendChild(infoBox);
|
||||
modal.appendChild(continueBtn);
|
||||
|
||||
container.appendChild(modal);
|
||||
|
||||
shadow.appendChild(style);
|
||||
shadow.appendChild(container);
|
||||
document.body.appendChild(host);
|
||||
activeModalHost = host;
|
||||
|
||||
function closeModal() {
|
||||
if (host && host.parentNode) {
|
||||
host.remove();
|
||||
activeModalHost = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Close button
|
||||
shadow.getElementById("mf-close").addEventListener("click", (e) => {
|
||||
e.stopPropagation();
|
||||
closeModal();
|
||||
});
|
||||
|
||||
// Close when clicking outside modal box
|
||||
container.addEventListener("click", (e) => {
|
||||
if (e.target === container) {
|
||||
closeModal();
|
||||
}
|
||||
});
|
||||
|
||||
// Continue button
|
||||
shadow.getElementById("mf-continue").addEventListener("click", (e) => {
|
||||
e.stopPropagation();
|
||||
browserAPI.runtime.sendMessage({
|
||||
action: "open_app",
|
||||
magnet: magnetUrl
|
||||
});
|
||||
closeModal();
|
||||
});
|
||||
|
||||
// Handle escape key
|
||||
const onKeyDown = (e) => {
|
||||
if (e.key === "Escape") {
|
||||
closeModal();
|
||||
window.removeEventListener("keydown", onKeyDown);
|
||||
}
|
||||
};
|
||||
window.addEventListener("keydown", onKeyDown);
|
||||
}
|
||||
|
||||
function escapeHTML(str) {
|
||||
if (!str) return "";
|
||||
return str
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """)
|
||||
.replace(/'/g, "'");
|
||||
}
|
||||
|
||||
// Hook magnet links via click event interception (capture phase)
|
||||
document.addEventListener(
|
||||
"click",
|
||||
(event) => {
|
||||
const target = event.target;
|
||||
if (!target) return;
|
||||
|
||||
const anchor = target.closest ? target.closest('a[href^="magnet:"]') : null;
|
||||
if (anchor) {
|
||||
const href = anchor.getAttribute("href") || anchor.href;
|
||||
if (href && href.startsWith("magnet:")) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
|
||||
// Inform background script of captured magnet
|
||||
browserAPI.runtime.sendMessage({
|
||||
action: "magnet_captured",
|
||||
magnet: href
|
||||
});
|
||||
|
||||
// Show popup modal on the page
|
||||
showMagnetModal(href);
|
||||
}
|
||||
}
|
||||
},
|
||||
true
|
||||
);
|
||||
})();
|
||||
Reference in new issue
Block a user