Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Expand URLs and remove UTMs #161

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ We use Twitter a lot and notice many dumb annoyances we'd like to fix. So here b
- Keyboard shortcut to toggle Night Mode (<kbd>Alt</kbd><kbd>m</kbd>).
- Uses your personal color theme on all profiles.
- Hides the header image on profile pages.
- Expand URLs and remove UTMs.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a better wording than UTMs. Few know what that means.

- [Shows alternative image text below images when available.](https://user-images.githubusercontent.com/170270/40556400-b46c292c-6076-11e8-8241-f5c4e1a7a161.png)

Tip: Twitter has a native [dark mode](https://github.com/sindresorhus/refined-twitter/issues/10) and you can toggle it using <kbd>Alt</kbd><kbd>m</kbd>. And press <kbd>Shift</kbd> <kbd>?</kbd> to see all keyboard shortcuts.
Expand Down
2 changes: 2 additions & 0 deletions source/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function onDomReady() {
enableFeature(features.cleanNavbarDropdown);
enableFeature(features.keyboardShortcuts);
enableFeature(features.preserveTextMessages);
enableFeature(features.expandUrlsRemoveUtms);

onRouteChange(() => {
enableFeature(features.autoLoadNewTweets);
Expand All @@ -81,6 +82,7 @@ function onDomReady() {
enableFeature(features.hidePromotedTweets);
enableFeature(features.renderInlineCode);
enableFeature(features.imageAlternatives);
enableFeature(features.expandUrlsRemoveUtms);
});
});

Expand Down
13 changes: 13 additions & 0 deletions source/features/expand-urls-remove-utms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function removeUTMs(url) {
return url.replace(/[?&#]utm_.*/, '');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to extract use the URL and URLSearchParams APIs to handle the query strings properly. Naive replacements like this is fragile.

}

export default function () {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The feature needs to ensure it's only run once. Now it's run on everything for each new tweet.

const urls = $('a[data-expanded-url]');

for (const el of urls) {
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
const expandedUrl = el.getAttribute('data-expanded-url');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use .dataset

const urlWithOutUtms = removeUTMs(expandedUrl);
el.setAttribute('href', urlWithOutUtms);
}
}
6 changes: 6 additions & 0 deletions source/features/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export const features = {
label: 'Preserve unsent text in the Messages modal when it closes',
fn: require('./preserve-text-messages').default
},
expandUrlsRemoveUtms: {
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
id: 'expand-urls-remove-utms',
category: 'general',
label: 'Expand t.co to the real urls without UTMs',
fn: require('./expand-urls-remove-utms').default
},
useSystemFont: {
id: 'feature-use-system-font',
category: 'general',
Expand Down