diff --git a/manifest.json b/manifest.json index 85f8012..64bfbd6 100644 --- a/manifest.json +++ b/manifest.json @@ -36,5 +36,19 @@ "" ] } - ] + ], + "commands": { + "execute-alt-r": { + "suggested_key": { + "default": "Alt+R" + }, + "description": "Execute Alt+R shortcut" + }, + "execute-ctrl-c": { + "suggested_key": { + "default": "Ctrl+C" + }, + "description": "Execute Ctrl+C shortcut" + } + } } diff --git a/scripts/content.js b/scripts/content.js index a86221b..9cf089a 100644 --- a/scripts/content.js +++ b/scripts/content.js @@ -88,7 +88,8 @@ parts.unshift(selector); element = element.parentNode; } - return parts.length ? '/' + parts.join('/') : null; + // return parts.length ? '/' + parts.join('/') : null; + return "/xpath"; } // 监听鼠标移动事件 @@ -131,6 +132,81 @@ } }); + // Function to remove highlight effect + function removeHighlightEffect() { + + if (previousHighlight) { + previousHighlight.style.background = ''; + previousHighlight.style.backgroundImage = ''; + previousHighlight.style.border = ''; + copyButton.style.display = 'none'; + previousHighlight = null; + } + } + + // Listen for mouse entering and leaving iframes + function addIframeMouseListeners() { + const iframes = document.querySelectorAll('iframe'); + iframes.forEach(iframe => { + iframe.addEventListener('mouseenter', function() { + console.log('Mouse entered iframe:', iframe.src); + removeHighlightEffect(); + }, true); + iframe.addEventListener('mouseleave', function() { + console.log('Mouse left iframe:', iframe.src); + removeHighlightEffect(); + }, true); + }); + } + + // Call the function to add listeners to existing iframes + addIframeMouseListeners(); + + // Also add listeners to any new iframes that are added to the DOM + const observer = new MutationObserver(() => { + addIframeMouseListeners(); + }); + + observer.observe(document.body, { childList: true, subtree: true }); + + // Function to enable copy mode in the current window and all iframes + function enableCopyModeInAllFrames() { + isCopyModeEnabled = true; + console.log('Copy mode enabled in main window'); + const iframes = document.querySelectorAll('iframe'); + iframes.forEach(iframe => { + iframe.contentWindow.postMessage({ action: 'enableCopyMode' }, '*'); + iframe.contentWindow.addEventListener('mouseleave', function() { + console.log('Mouse left iframe:', iframe.src); + removeHighlightEffect(); + }); + }); + } + + // Function to disable copy mode in the current window and all iframes + function disableCopyModeInAllFrames() { + isCopyModeEnabled = false; + removeHighlightEffect(); + console.log('Copy mode disabled in main window'); + const iframes = document.querySelectorAll('iframe'); + iframes.forEach(iframe => { + iframe.contentWindow.postMessage({ action: 'disableCopyMode' }, '*'); + }); + } + + // Listen for messages from iframes to enable or disable copy mode + window.addEventListener('message', function (event) { + if (event.data.action === 'enableCopyMode') { + isCopyModeEnabled = true; + console.log('Copy mode enabled in iframe'); + } else if (event.data.action === 'disableCopyMode') { + isCopyModeEnabled = false; + removeHighlightEffect(); + console.log('Copy mode disabled in iframe'); + } + console.log('Message received from iframe:', event.origin); + }); + // 监听消息以启用或禁用拷贝模式 chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { if (request.action === 'toggleCopyMode') { @@ -149,21 +225,13 @@ document.addEventListener('keydown', function (event) { console.log(event.key); if (event.altKey && event.key === 'r') { - isCopyModeEnabled = true; - console.log('Copy mode enabled'); + enableCopyModeInAllFrames(); } else if (event.key === 'Escape') { - isCopyModeEnabled = false; - if (previousHighlight) { - previousHighlight.style.background = ''; - previousHighlight.style.backgroundImage = ''; - previousHighlight.style.border = ''; - copyButton.style.display = 'none'; - previousHighlight = null; - } - console.log('Copy mode disabled'); + disableCopyModeInAllFrames(); } else if (event.ctrlKey && event.key === 'c' && previousHighlight) { const xpath = getElementXPath(previousHighlight); const action = previousHighlight.tagName.toLowerCase() === 'input' ? 'input("your_text")' : 'click()'; + const command = `web.find_by_xpath("${xpath}").${action}`; navigator.clipboard.writeText(command).then(() => { console.log('Command copied to clipboard:', command);