Bladeren bron

Changed placeholder to span

Changed the placeholder linkAddress to span.
Added a logging flag and set it to false for production.
This improves speed.
Dhruv Vemula 13 jaren geleden
bovenliggende
commit
5391381e8b
1 gewijzigde bestanden met toevoegingen van 41 en 16 verwijderingen
  1. 41 16
      copylinkaddress.js

+ 41 - 16
copylinkaddress.js

@@ -1,41 +1,66 @@
-var linkAddress = $('<input id="copylAddress" type="text" />');
+var linkAddress = $('<span id="copylAddress" />');
 $('body').append(linkAddress);
 // 'Hidden' yet selectable
-linkAddress.css({position: 'absolute', left:'-9999em'});
+//linkAddress.css({position: 'absolute', left:'-9999em'});
+
+copylLogging = false;
+
+function selectElement(el) {
+    var range = document.createRange();
+    console.log(el[0]);
+    range.selectNodeContents(el[0]);
+    window.getSelection().addRange(range);
+}
 
 function clearLinkAddress() {
-    if (linkAddress.val()) {
-        linkAddress.val(null);
-//        linkAddress.blur();
-        console.log("Cleared linkAddress");
+    if (linkAddress.text()) {
+        linkAddress.text("");
+        linkAddress.blur();
+        if (copylLogging) {
+            console.log("Cleared linkAddress");
+        }
         window.getSelection().removeAllRanges();
     }
-    console.log("Current selection: " + window.getSelection().toString());
+    if (copylLogging) {
+        console.log("Current selection: " + window.getSelection().toString());
+    }
 }
 
 $(function() {
     $('a').on({
         mouseenter: function() {
             if(window.getSelection().toString()) {
-                console.log("Something is already selected. Copyl backs out.");
+                if (copylLogging) {
+                    console.log("Something is already selected. Copyl backs out.");
+                }
             } else {
-                console.log("Nothing is selected. Copyl kicks in.");
-                linkAddress.val($(this).prop('href'));
-                linkAddress.select();
-                linkAddress.blur();
-                console.log("linkAddress: " + linkAddress.val());
+                if (copylLogging) {
+                    console.log("Nothing is selected. Copyl kicks in.");
+                }
+                linkAddress.text($(this).prop('href'));
+                selectElement(linkAddress);
+                if (copylLogging) {
+                    console.log("linkAddress: " + linkAddress.text());
+                }
+            }
+            if (copylLogging) {
+                console.log("Current Selection: " + window.getSelection().toString());
             }
-            console.log("Current Selection: " + window.getSelection().toString());
         },
         mouseleave: function() {
-            console.log("Leaving link.");
+            if (copylLogging) {
+                console.log("Leaving link.");
+            }
             clearLinkAddress();
         }
     });
 
     // Clear linkAddress when user closes or moves away from the page
+    // Does not work in Chrome 14+
     window.onbeforeunload = function() {
-        console.log("Leaving page.");
+        if (copylLogging) {
+            console.log("Leaving page.");
+        }
         clearLinkAddress();
     }
 });