Skip to main content
replaced http://meta.stackoverflow.com/ with https://meta.stackoverflow.com/
Source Link

This script is now unnecessary in questions and answers, where raw links are replaced by the current question titleraw links are replaced by the current question title. However, this feature is [status-declined] for commentsthis feature is [status-declined] for comments and doesn't work in Chat, which also uses mini-markdown. Chat does have oneboxing, but you can't insert names inline without this script or a bunch of typing or copy-pasting.

httphttps://meta.stackoverflow.com/questions/89378/userscript-to-auto-replace-sofu-links-in-comments

Please change this to use the shorter http://meta.stackoverflow.com/q/89378https://meta.stackoverflow.com/q/89378 and http://meta.stackoverflow.com/q/89378/89389#89389https://meta.stackoverflow.com/q/89378/89389#89389 format. That seems like it would be a trivial Javascript string manipulation. Here's an attempt, I make no assumptions as to its validity, speed, or idiomatic-ness:

function AssertException(message) { this.message = message; }
AssertException.prototype.toString = function () {
  return 'AssertException: ' + this.message;
}

function assert_equal(expression, result) {
  if (expression != result) {
    throw new AssertException('Expression produced "' .. expression .. '" which did not equal "' .. result .. '".');
  }
}

 // Test function:
function verify_minify()
{

    assert_equal('test_str',
                 'test_str');
                      
    // Standard Trilogy question
    assert_equal(minify('http://stackoverflow.com/question/12345/some-text'),
                      'http://stackoverflow.com/q/12345');
    //With answer ID:
    assert_equal(minify('http://stackoverflow.com/q/12345/some-text/12346#12346'),
                      'http://stackoverflow.com/q/12345/12346#12346');
    //With Meta prefix:
    assert_equal(minify('http'https://meta.stackoverflow.com/question/12345/some-text'),
                      'http'https://meta.stackoverflow.com/q/12345');
    //With Meta prefix and answer ID:
    assert_equal(minify('http'https://meta.stackoverflow.com/q/12345/some-text/12346#12346'),
                      'http'https://meta.stackoverflow.com/q/12345/12346#12346');
    //With site.stackexchange format: 
    assert_equal(minify('http://electronics.stackexchange.com/q/12345/some-text'),
                      'http://electronics.stackexchange.com/q/12345');
    //With site.stackexchange format and answer ID: 
    assert_equal(minify('http://electronics.stackexchange.com/q/12345/some-text/12346#12346'),
                      'http://electronics.stackexchange.com/q/12345/12346#12346');
    //With site.stackexchange format, meta prefix, and answer ID: 
    assert_equal(minify('http://meta.electronics.stackexchange.com/q/12345/some-text/12346#12346'),
                      'http://meta.electronics.stackexchange.com/q/12345/12346#12346');

    alert('Tests passed.');

}

This script is now unnecessary in questions and answers, where raw links are replaced by the current question title. However, this feature is [status-declined] for comments and doesn't work in Chat, which also uses mini-markdown. Chat does have oneboxing, but you can't insert names inline without this script or a bunch of typing or copy-pasting.

http://meta.stackoverflow.com/questions/89378/userscript-to-auto-replace-sofu-links-in-comments

Please change this to use the shorter http://meta.stackoverflow.com/q/89378 and http://meta.stackoverflow.com/q/89378/89389#89389 format. That seems like it would be a trivial Javascript string manipulation. Here's an attempt, I make no assumptions as to its validity, speed, or idiomatic-ness:

function AssertException(message) { this.message = message; }
AssertException.prototype.toString = function () {
  return 'AssertException: ' + this.message;
}

function assert_equal(expression, result) {
  if (expression != result) {
    throw new AssertException('Expression produced "' .. expression .. '" which did not equal "' .. result .. '".');
  }
}

 // Test function:
function verify_minify()
{

    assert_equal('test_str',
                 'test_str');
                      
    // Standard Trilogy question
    assert_equal(minify('http://stackoverflow.com/question/12345/some-text'),
                      'http://stackoverflow.com/q/12345');
    //With answer ID:
    assert_equal(minify('http://stackoverflow.com/q/12345/some-text/12346#12346'),
                      'http://stackoverflow.com/q/12345/12346#12346');
    //With Meta prefix:
    assert_equal(minify('http://meta.stackoverflow.com/question/12345/some-text'),
                      'http://meta.stackoverflow.com/q/12345');
    //With Meta prefix and answer ID:
    assert_equal(minify('http://meta.stackoverflow.com/q/12345/some-text/12346#12346'),
                      'http://meta.stackoverflow.com/q/12345/12346#12346');
    //With site.stackexchange format: 
    assert_equal(minify('http://electronics.stackexchange.com/q/12345/some-text'),
                      'http://electronics.stackexchange.com/q/12345');
    //With site.stackexchange format and answer ID: 
    assert_equal(minify('http://electronics.stackexchange.com/q/12345/some-text/12346#12346'),
                      'http://electronics.stackexchange.com/q/12345/12346#12346');
    //With site.stackexchange format, meta prefix, and answer ID: 
    assert_equal(minify('http://meta.electronics.stackexchange.com/q/12345/some-text/12346#12346'),
                      'http://meta.electronics.stackexchange.com/q/12345/12346#12346');

    alert('Tests passed.');

}

This script is now unnecessary in questions and answers, where raw links are replaced by the current question title. However, this feature is [status-declined] for comments and doesn't work in Chat, which also uses mini-markdown. Chat does have oneboxing, but you can't insert names inline without this script or a bunch of typing or copy-pasting.

https://meta.stackoverflow.com/questions/89378/userscript-to-auto-replace-sofu-links-in-comments

Please change this to use the shorter https://meta.stackoverflow.com/q/89378 and https://meta.stackoverflow.com/q/89378/89389#89389 format. That seems like it would be a trivial Javascript string manipulation. Here's an attempt, I make no assumptions as to its validity, speed, or idiomatic-ness:

function AssertException(message) { this.message = message; }
AssertException.prototype.toString = function () {
  return 'AssertException: ' + this.message;
}

function assert_equal(expression, result) {
  if (expression != result) {
    throw new AssertException('Expression produced "' .. expression .. '" which did not equal "' .. result .. '".');
  }
}

 // Test function:
function verify_minify()
{

    assert_equal('test_str',
                 'test_str');
                      
    // Standard Trilogy question
    assert_equal(minify('http://stackoverflow.com/question/12345/some-text'),
                      'http://stackoverflow.com/q/12345');
    //With answer ID:
    assert_equal(minify('http://stackoverflow.com/q/12345/some-text/12346#12346'),
                      'http://stackoverflow.com/q/12345/12346#12346');
    //With Meta prefix:
    assert_equal(minify('https://meta.stackoverflow.com/question/12345/some-text'),
                      'https://meta.stackoverflow.com/q/12345');
    //With Meta prefix and answer ID:
    assert_equal(minify('https://meta.stackoverflow.com/q/12345/some-text/12346#12346'),
                      'https://meta.stackoverflow.com/q/12345/12346#12346');
    //With site.stackexchange format: 
    assert_equal(minify('http://electronics.stackexchange.com/q/12345/some-text'),
                      'http://electronics.stackexchange.com/q/12345');
    //With site.stackexchange format and answer ID: 
    assert_equal(minify('http://electronics.stackexchange.com/q/12345/some-text/12346#12346'),
                      'http://electronics.stackexchange.com/q/12345/12346#12346');
    //With site.stackexchange format, meta prefix, and answer ID: 
    assert_equal(minify('http://meta.electronics.stackexchange.com/q/12345/some-text/12346#12346'),
                      'http://meta.electronics.stackexchange.com/q/12345/12346#12346');

    alert('Tests passed.');

}
Source Link

This script is now unnecessary in questions and answers, where raw links are replaced by the current question title. However, this feature is [status-declined] for comments and doesn't work in Chat, which also uses mini-markdown. Chat does have oneboxing, but you can't insert names inline without this script or a bunch of typing or copy-pasting.

If my terrible JavaScript skills are correct, you're currently using location.href to determine the page. This text is something like:

http://meta.stackoverflow.com/questions/89378/userscript-to-auto-replace-sofu-links-in-comments

That's 96 characters, a fairly long URL (but not maximally long, that's over 120 characters!). Unfortunately, the places where this script is necessary and useful (chat and comments) both have character limits.

Please change this to use the shorter http://meta.stackoverflow.com/q/89378 and http://meta.stackoverflow.com/q/89378/89389#89389 format. That seems like it would be a trivial Javascript string manipulation. Here's an attempt, I make no assumptions as to its validity, speed, or idiomatic-ness:

function minify(url) {

    var question_re = /(^\D+)question\/(\d+)/;

    var answer_re = /\/\d+#\d+$/;
    if (re.test(answer_re)) {
       return url.replace(question_re, '$1q$2') .. url.replace(answer_re);
    } else {
       return url.replace(qestion_re, '$1q$2');
    }
}

and a testcase.

function AssertException(message) { this.message = message; }
AssertException.prototype.toString = function () {
  return 'AssertException: ' + this.message;
}

function assert_equal(expression, result) {
  if (expression != result) {
    throw new AssertException('Expression produced "' .. expression .. '" which did not equal "' .. result .. '".');
  }
}

 // Test function:
function verify_minify()
{

    assert_equal('test_str',
                 'test_str');
                      
    // Standard Trilogy question
    assert_equal(minify('http://stackoverflow.com/question/12345/some-text'),
                      'http://stackoverflow.com/q/12345');
    //With answer ID:
    assert_equal(minify('http://stackoverflow.com/q/12345/some-text/12346#12346'),
                      'http://stackoverflow.com/q/12345/12346#12346');
    //With Meta prefix:
    assert_equal(minify('http://meta.stackoverflow.com/question/12345/some-text'),
                      'http://meta.stackoverflow.com/q/12345');
    //With Meta prefix and answer ID:
    assert_equal(minify('http://meta.stackoverflow.com/q/12345/some-text/12346#12346'),
                      'http://meta.stackoverflow.com/q/12345/12346#12346');
    //With site.stackexchange format: 
    assert_equal(minify('http://electronics.stackexchange.com/q/12345/some-text'),
                      'http://electronics.stackexchange.com/q/12345');
    //With site.stackexchange format and answer ID: 
    assert_equal(minify('http://electronics.stackexchange.com/q/12345/some-text/12346#12346'),
                      'http://electronics.stackexchange.com/q/12345/12346#12346');
    //With site.stackexchange format, meta prefix, and answer ID: 
    assert_equal(minify('http://meta.electronics.stackexchange.com/q/12345/some-text/12346#12346'),
                      'http://meta.electronics.stackexchange.com/q/12345/12346#12346');

    alert('Tests passed.');

}

Note: I didn't actually run any of these except to verify that the syntax highlighter in my text editor didn't break, I don't have any kind of a development environment set up and don't know how to do that.