Explorar o código

release v0.8.0: hit X to copy url text + simplyfy code a lot + don't de-focus text input fields anymore

Noah Vogt %!s(int64=2) %!d(string=hai) anos
pai
achega
d29e98ac7b
Modificáronse 3 ficheiros con 36 adicións e 113 borrados
  1. 17 5
      README.md
  2. 17 106
      copylinkaddress.js
  3. 2 2
      manifest.json

+ 17 - 5
README.md

@@ -1,9 +1,9 @@
 # Copy URL on Hover
-This is an extension for both chromium-based and firefox-based browsers, so you can hover over a link and hit Y to yank it *like a real vim user*! For feedback, a pop sound will be played after copying.
+This is an extension for both chromium-based and firefox-based browsers, so you can hover over a link and hit Y to copy (yank) the link address *like a real vim user* - or hit X to copy the link text! For feedback, a pop sound will be played after copying.
 
 If you need to copy link addresses more than once, you know how annoying it is. You need to right-click, find the "Copy link address" menu item, and click it. I find this not very efficient and overly complicated, which is why I use this extension for copying links.
 
-*Note:* This project is a fork from [dhruvtv/copylinkaddress](https://github.com/dhruvtv/copylinkaddress).
+This project is a originally a fork from [dhruvtv/copylinkaddress](https://github.com/dhruvtv/copylinkaddress), but now does have nearly no overlap of code anymore.
 
 ## Installation
 
@@ -11,11 +11,16 @@ If you need to copy link addresses more than once, you know how annoying it is.
 To try the latest dev version of the extension follow the steps below.
 
 1. Download the source (either hit the 'ZIP' button you see at the top of the page, or use `git clone`) and extract it to a directory.
-2. In your chromium-based browser, launch `chrome://extensions`
+2. In your chromium-based browser, launch `chrome://extensions` (note: instead of 'chrome' it could be something different like 'brave')
 3. Enable 'Developer Mode'.
 4. Click 'Load Unpacked Extensions' and point to the above directory.
 
-I will see when a chrome web store version will be available, as i don't want to show give google my ID and pay them 5 USD via credit card.
+I will see when a chrome web store version will be available, as I don't want to show google my ID and pay them 5 USD via credit card.
+
+If you use Arch Linux, you can check out the following packages in the AUR.
+
+- source-based AUR package: [chromium-extension-copy-url-on-hover](https://aur.archlinux.org/packages/chromium-extension-copy-url-on-hover/)
+- binary AUR package: [chromium-extension-copy-url-on-hover-bin](https://aur.archlinux.org/packages/chromium-extension-copy-url-on-hover-bin/)
 
 ### Firefox
 You can add the newest via the [firefox add-on on store](https://addons.mozilla.org/en-US/firefox/addon/copy-url-on-hover).
@@ -23,4 +28,11 @@ You can add the newest via the [firefox add-on on store](https://addons.mozilla.
 As you cannot persistently add add-ons to vanilla firefox without using a file that is signed from mozilla, this makes it very much unusable to install a dev version. You would have to install the firefox 'developer edition', and set some about:config flags.
 
 ## Known Issue
-For my this bug is not really a problem, but for you it may be one: ***Hovering over a link will make focused text input fields lose focus*** (sometimes only temporarily - focus should be restored when you move away from the link). This fault is not yet resolved due to my very limited JavaScript Skills, but if you claim to be a "modern web developer" you are happily invited to fix this issue, of course.
+For my this bug is not really a problem, but for you it may be one: ***Hovering over a link while having focused text input fields will not block your input of pressing X or Y*** . This is a design choice, in previous versions of this software the input was blocked, but the text field lost their focus. The current design is also not considered optimal, and may be subject to change, but it is perceived as the more intuitive behaviour. If you have a better idea, please contact me.
+
+## Roadmap
+
+- find a cleaner solution for the above mentioned known issue
+- migrate to manifest v3
+- show a popup notification when copying a link address/text
+- add settings panel to toggle the sound and notification

+ 17 - 106
copylinkaddress.js

@@ -1,112 +1,23 @@
-/*
-The way the extension works is, once you hover a link:
-If something is already selected in the page, it does nothing. Else, it
-takes the URL of the link you are hovering, copies it to an invisible span,
-programmatically selects the span.
+let popSoundUrl = chrome.runtime.getURL('sounds/pop.mp3');
+let popSoundAudio = new Audio(popSoundUrl);
 
-Now when you hits Cmd-C (Ctrl-C), the hidden selection is copied to clipboard.
-
-When you move pointer away from the link, it
-clears the hidden selection, clears the invisible span.
-
-If, at the time of hover, the cursor was in a textbox (without anything selected),
-it is technically a zero-length selection in Chrome. So, the extension goes ahead and clears that selection
-(thereby taking the cursor away from the textbox), saving the caret position.
-When you move away from the link, the caret position is restored.
-*/
-
-COPYL_DEBUG = false;
-
-let url = chrome.runtime.getURL('sounds/pop.mp3');
-let a = new Audio(url);
-var previousCaretPosition = -1;
-var linkAddress = $('<span id="copylAddress" style="display: inline-block;" />');
-$('body').append(linkAddress);
-/* This is a DOM element that has to be selectable but not visible to anybody */
-linkAddress.css({position: 'absolute', left:'-9999em'});
+function getAbsoluteURL(url) {
+    var link = document.createElement("a");
+    link.href = url;
+    return (link.protocol + "//" + link.host + link.pathname);
+}
 
-/* when the user presses y (=yank), copy URL to clipboard when hovering */
 window.onkeydown = function(event) {
-    if (event.key == "y") {
-        if (linkAddress.text().length != 0) {
-			document.execCommand('copy');
-            a.play();
+    var hoveredLink = $('a:hover');
+    if (hoveredLink.length) {
+        if (event.key == "x") {
+            navigator.clipboard.writeText(hoveredLink.text())
+        } else if (event.key == "y") {
+            var hoveredHrefAttr = hoveredLink.attr('href');
+            navigator.clipboard.writeText(getAbsoluteURL(hoveredHrefAttr));
+        } else {
+            return;
         }
+        popSoundAudio.play();
     }
 }
-
-
-
-function write_to_console(text) {
-    if (COPYL_DEBUG)
-        console.log(text);
-}
-
-function selectElement(el) {
-    /* Check if anything is currently selected, if so backup */
-    if (window.getSelection().rangeCount > 0) {
-        previousCaretPosition = document.activeElement.selectionStart;
-    }
-    write_to_console("Previous cursor position: " + previousCaretPosition);
-    var range = document.createRange();
-
-    write_to_console("Selecting element " + el);
-    write_to_console("el[0] is " + el[0]);
-    range.selectNodeContents(el[0]);
-    write_to_console("Range: " + range);
-
-    write_to_console("Window selection range count before: " + window.getSelection().rangeCount);
-    if (window.getSelection().rangeCount > 0) {
-        window.getSelection().removeAllRanges();
-    }
-    write_to_console("Window selection range count now: " + window.getSelection().rangeCount);
-
-    write_to_console("Active Element: " + document.activeElement);
-    window.getSelection().addRange(range);
-    write_to_console("Window selection range count after: " + window.getSelection().rangeCount);
-}
-
-function clearLinkAddress() {
-    if (linkAddress.text()) {
-        linkAddress.text("");
-        linkAddress.blur();
-        write_to_console("Cleared linkAddress");
-
-        write_to_console("window.getSelection: " + window.getSelection());
-        window.getSelection().removeAllRanges();
-    }
-    if (previousCaretPosition != -1)
-    {
-        document.activeElement.selectionStart = previousCaretPosition;
-        write_to_console("Previous cursor position set: " + document.activeElement.selectionStart);
-    }
-
-    write_to_console("Current selection: " + window.getSelection().toString());
-}
-
-$(function() {
-    /* The code attaches itself to all anchor elements */
-    $("html").on("mouseenter", "a", function(){
-        /* Everytime the user hovers (enters) a link */
-        if(window.getSelection().toString()) {
-            write_to_console("Something is already selected. Don't do anything.");
-        } else {
-            write_to_console("Nothing is selected.");
-
-            linkAddress.text($(this).prop('href'));
-            write_to_console("linkAddress: " + linkAddress.text());
-            selectElement(linkAddress);
-        }
-
-        write_to_console("Current Selection: " + window.getSelection().toString());
-    }).on("mouseleave", "a", function(){
-            /* Every time the user leaves a link */
-            write_to_console("Leaving link.");
-            clearLinkAddress();
-        });
-
-    $(window).on("beforeunload", function() {
-        clearLinkAddress();
-    });
-});
-

+ 2 - 2
manifest.json

@@ -1,8 +1,8 @@
 {
     "manifest_version": 2,
     "name": "Copy URL on Hover",
-    "version": "0.7.0",
-    "description": "Hover over a link and hit Y to yank (copy) it like a real vim user! For feedback, a pop sound will be played after copying.",
+    "version": "0.8.0",
+    "description": "Hover over a link and hit Y to copy (yank) the link address - or hit X to copy the link text! For feedback, a pop sound will be played after copying.",
     "content_scripts": [
         {
         "matches": ["<all_urls>"],