Mte90 / facebook-direct-links

Say bye to redirect on Facebook with this very simple extension!
GNU General Public License v3.0
16 stars 7 forks source link

Feature request: Not just working on external links but also work on Facebook links #28

Open PeterDaveHello opened 3 years ago

PeterDaveHello commented 3 years ago

The links of Facebook itself is currently not cleaned up here, it'll be great the links of Facebook itself can be more "direct"?

PeterDaveHello commented 3 years ago

Maybe we should remove the condition of external link, it'll be great the links of Facebook itself can be more "direct"? https://github.com/Mte90/facebook-direct-links/blob/39809822009ef71e65f0bce4f5ad9fab74d8e593/direct.js#L89

PeterDaveHello commented 3 years ago

It's interesting that I just found the method to remove external link condition could be too aggressive, that some of the hashtags in the posts could be broken (the link will be missing).

Mte90 commented 3 years ago

Yes there are some parameters that are needed for "internal links" so that the reason of that check, this require a bit of testing but usually it is better to not remove it. I don't know with the new UI if things are changed.

PeterDaveHello commented 3 years ago

@Mte90 I got a working patch here, not a very beautify solution, and not sure if that's something acceptable here, would like to have some comments from you:

diff --git a/direct.js b/direct.js
index 5735362..f1993ce 100644
--- a/direct.js
+++ b/direct.js
@@ -47,7 +47,7 @@
     win.ready = ready;

     ready('a', function (element) {
-        let cleanup = function() {
+        let externalCleanup = function() {
             let uri = element.href;

             if (/^https?:\/\/lm?.facebook.com/i.test(uri)) {
@@ -77,8 +77,31 @@
             element.href = uri;
         }

-        let eventBlocker = function(evt) {
-            cleanup();
+        let internalCleanup = function() {
+            let uri = element.href;
+
+            uri = decodeURIComponent(uri);
+            uri = uri.replace(/([&|?])__cft__\[0\]=[^&#$/]*/gi, '$1');
+            uri = uri.replace(/([&|?])__tn__=[^&#$/]*/gi, '$1');
+            uri = uri.replace(/([&|?])__eep__=[^&#$/]*/gi, '$1');
+
+            // Additional `&` clean up
+            uri = uri.replace(/([&|?])(&+)/gi, '$1');
+
+            if (uri[uri.length -1] === '?') {
+                uri = uri.substr(0, uri.length-1);
+            }
+
+            element.href = uri;
+        }
+
+        let eventBlockerForExtLinks = function(evt) {
+            externalCleanup();
+            evt.stopImmediatePropagation();
+        }
+
+        let eventBlockerForIntLinks = function(evt) {
+            externalCleanup();
             evt.stopImmediatePropagation();
         }

@@ -91,12 +114,19 @@
         var trackerLinkRegex = /^https?:\/\/lm?.(facebook\.com|facebookwww\.onion|facebookwkhpilnemxj7asaniu7vnjjbiltxjqhye3mhbshg7kx5tfyd\.onion)\/l.php\?u=([^&#$]+)/i;

         if( !domainfilter.includes(domain) || trackerLinkRegex.test(url) ) { //external links
-            element.addEventListener('click', eventBlocker);
-            element.addEventListener('contextmenu', eventBlocker);
-            element.addEventListener('touchstart', eventBlocker);
-            element.addEventListener('mousedown', eventBlocker);
-            element.addEventListener('mouseup', eventBlocker);
-            cleanup();
+            element.addEventListener('click', eventBlockerForExtLinks);
+            element.addEventListener('contextmenu', eventBlockerForExtLinks);
+            element.addEventListener('touchstart', eventBlockerForExtLinks);
+            element.addEventListener('mousedown', eventBlockerForExtLinks);
+            element.addEventListener('mouseup', eventBlockerForExtLinks);
+            externalCleanup();
+        } else {
+            element.addEventListener('click', eventBlockerForIntLinks);
+            element.addEventListener('contextmenu', eventBlockerForIntLinks);
+            element.addEventListener('touchstart', eventBlockerForIntLinks);
+            element.addEventListener('mousedown', eventBlockerForIntLinks);
+            element.addEventListener('mouseup', eventBlockerForIntLinks);
+            internalCleanup();
         }
     });
Mte90 commented 3 years ago

It is missing the externalCleanup function but seems a simple way to achieve what we need. To me is fine ;-)

PeterDaveHello commented 3 years ago

@Mte90 externalCleanup() is not touched, that's why it's not in the diff result ;)

Mte90 commented 3 years ago

ok seems perfect!

PeterDaveHello commented 2 years ago

@Mte90 I thought #30 was reverted by #32, so this issue is not resolved?

Mte90 commented 2 years ago

I didn't remember, I saw no updates in the ticket and I thought that was closed.

PeterDaveHello commented 2 years ago

Currently that feature is not implemented yet, so maybe I just reopen it?