Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 98359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:10:51+00:00 2026-05-11T00:10:51+00:00

I have the following (fairly) simple JavaScript snippet that I have wired into Greasemonkey.

  • 0

I have the following (fairly) simple JavaScript snippet that I have wired into Greasemonkey. It goes through a page, looks for <a> tags whose href points to tinyurl.com, and adds a ‘title’ attribute that identifies the true destination of the link. Much of the important code comes from an older (unsupported) Greasemonkey script that quits working when the inner component that held the XPath implementation changed. My script:

(function() {     var providers = new Array();     providers['tinyurl.com'] = function(link, fragment) {         // This is mostly taken from the (broken due to XPath component         // issues) tinyurl_popup_preview script.         link.title = 'Loading...';         GM_xmlhttpRequest({                 method: 'GET',                 url: 'http://preview.tinyurl.com/' + fragment,                 onload: function(res) {                     var re = res.responseText.match('<blockquote><b>(.+)</b></blockquote>');                     if (re)                     {                         link.title = re[1].replace(/\<br \/\>/g, '').replace(/&amp;/g, '&');                     }                     else                     {                         link.title = 'Parsing failed...';                     }                 },                 onerror: function() {                     link.title = 'Connection failed...';                 }         });     };     var uriPattern = /(tinyurl\.com)\/([a-zA-Z0-9]+)/;     var aTags = document.getElementsByTagName('a');      for (i = 0; i < aTags.length; i++)     {         var data = aTags[i].href.match(uriPattern);         if (data != null && data.length > 1 && data[2] != 'preview')         {             var source = data[1];             var fragment = data[2];             var link = aTags[i];             aTags[i].addEventListener('mouseover', function() {                 if (link.title == '')                 {                     (providers[source])(link, fragment);                 }             }, false);         }     } })(); 

(The reason the ‘providers’ associative array is set up the way it is, is so that I can expand this to cover other URL-shortening services as well.)

I have verified that all the various branches of code are being reached correctly, in cases where the link being examined does and does not match the pattern. What isn’t happening, is any change to the ‘title’ attribute of the anchor tags. I’ve watched this via Firebug, thrown alert() calls in left and right, and it just never changes. In a previous iteration all expressions of the form:

link.title = '...'; 

had originally been:

link.setAttribute('title', '...'); 

That didn’t work, either. I’m no newbie to JavaScript OR Greasemonkey, but this one has me stumped!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:10:52+00:00Added an answer on May 11, 2026 at 12:10 am

    Try replacing the body of the if with this code instead.

            aTags[i].addEventListener('mouseover', (function(source, fragment)         {             return function()             {                 if (this.title == '')                 {                     (providers[source])(this, fragment);                 }             }         })(data[1], data[2]), false) ;  

    Note after the loop is complete do aTags = null;.

    Your problem is that an if statement block is not a true scope, anything var’d will belong to the outer functions scope. Hence your inner function that you are providing as an event handler would use the source, link and fragment of the last pass. Additionaly by maintaining references to DOM object you would have a memory leak due to circular references.

    The above approach create a new scope on each pass via a function call so each source and fragment is in its own scope. It also uses the fact that a function called as an event listener has the this property pointing at the element to which it is attached, hence avoiding a circular reference containing a DOM element.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 261k
  • Answers 261k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Check out All Seeing Interactives ASIHTTPRequest library May 13, 2026 at 11:36 am
  • Editorial Team
    Editorial Team added an answer The click function binds when it is run. You need… May 13, 2026 at 11:36 am
  • Editorial Team
    Editorial Team added an answer One possibility is to create an XSLT transform that converts… May 13, 2026 at 11:36 am

Related Questions

I have a fairly simple ASP.NET 2.0 menu control using a sitemap file and
I have been using SQL for years, but have mostly been using the query
I have the following mod_rewrite rule: RewriteRule ^home/ home.php [L] Fairly simple. When I
I have what seems to be a fairly simple requirement, but looking around I'm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.