Discuss Scratch

TheSepticorn
Scratcher
13 posts

Link to Profile Userscript

I was bored and kind of annoyed that when people are mentioned on the forums using the @ symbol you aren't automatically given a link to their profile, so I made a userscript to do it!

It doesn't detect if the @ sign is followed by an actual user so it can end up linking you to a blank page. It's also kind of slow . Tell me if you have any suggestions!

// ==UserScript==
// @name Link to Profile
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Links you to a Scratcher's profile when they are mentioned with and at symbol on the forums
// @author TheSepticorn
// @match https://scratch.mit.edu/discuss/topic/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
var posts = document.getElementsByClassName("post_body_html");
for (var i = 0; i < posts.length; i++) {
var postText = posts[i].innerHTML;
var newPostText = "";
var char = 0;
var letters = /^[0-9a-zA-Z]|[-_]/;
while ( char < postText.length){
if (postText.charAt(char) == "@" && postText.charAt(char + 1).match(letters)){
var username = "";
char++;
while ((postText.charAt(char) != " " && postText.charAt(char).match(letters))&&(char < postText.length)){
username += postText.charAt(char);
char++;
}
newPostText += "<a href = \"https://scratch.mit.edu/users/" + username + "\">" + "@" + username + "</a>";
} else {
newPostText += postText.charAt(char);
char++;
}
}
posts[i].innerHTML = newPostText;
}
})();
bobby1171
Scratcher
100+ posts

Link to Profile Userscript

awesome! test: @bob1171
jokebookservice1
Scratcher
1000+ posts

Link to Profile Userscript

Hmm, this seems cool, however, your code could be rewritten to the shorter

Array.from(document.getElementsByClassName("post_body_html")).forEach(post => post.innerHTML = post.innerHTML.replace(/(@([a-zA-Z0-9_-]{2,}))/g, '<a href="https://scratch.mit.edu/users/$2/">$1</a>'));

(Of course with the ==Userscript== stuff above it).

It also matches stuff inside [code] tags, which should really be treated as code. Mine also ensures the username has at least two characters.

It might also be good to include signatures in the search?

Your code is really great, and well done! (Might I suggest looking at for loops to make your code more readable than using a while() loop?)

Edited – copied an old version of my code that didn't work. Fixed now.

Last edited by jokebookservice1 (Aug. 13, 2017 12:44:31)

TheSepticorn
Scratcher
13 posts

Link to Profile Userscript

jokebookservice1 wrote:

Hmm, this seems cool, however, your code could be rewritten to the shorter

Array.from(document.getElementsByClassName("post_body_html")).forEach(post => post.innerHTML = post.innerHTML.replace(/(@([a-zA-Z0-9_-]{2,}))/g, '<a href="https://scratch.mit.edu/users/$2/">$1</a>'));

(Of course with the ==Userscript== stuff above it).

It also matches stuff inside [code] tags, which should really be treated as code. Mine also ensures the username has at least two characters.

It might also be good to include signatures in the search?

Your code is really great, and well done! (Might I suggest looking at for loops to make your code more readable than using a while() loop?)

Edited – copied an old version of my code that didn't work. Fixed now.
Thanks! I thought there was probably a way to do it using a regex, but I'm not good at them .

Powered by DjangoBB