-2

I looked into google and found an approach of what I trying to do here here but now I'm stuck. I'm using jQuery to get the value of the onclick function, but I need some inner code out of the Info function.

Here is what I get from $('a.turqheadlabel'):

[<a href=​"#" onclick=​"Info('another-ending')​" class=​"TurqHeadLabel">​ANOTHER ENDING ​</a>​,
<a href=​"#" onclick=​"Info('good-ending')​" class=​"TurqHeadLabel">​GOOD ENDING ​</a>​ ]

I used $('a.turqheadlabel').attr('onclick') and I get this:

function onclick(event) {
    Info('another-ending')
}

What I look is get the another-ending and good-ending so I can put them in an array that I need to call.

PS. If there is a way to avoid using eval is preferable since chrome will block it next year.

Community
  • 1
  • 1
Braiam
  • 1
  • 11
  • 47
  • 78
  • What do you want to do with the extracted values? Chrome extensions do not have direct access to any global variables. So, even if you manage to get "another-ending" and "good-ending", you cannot just put `Info(value);` in the content script. The most effective solution really depends on your use case, what is it? – Rob W Sep 02 '12 at 19:36

1 Answers1

1

On a website you control

Assuming you want to access the value you're passing to Info ('good-ending' and 'another-ending') then you'd be better off using HTML data attributes and accessing the values using jQuery's data function.

If you re-write your HTML to look like this:

<a href='#' class='TurqHeadLabel' data-info='some-ending'>SOME ENDING</a>

Then you can add your onclick handlers dynamically, and also easily access your info values in other places. This will attach the onclick handlers when the DOM is ready:

jQuery(function ($) {
    $('a.TurqHeadLabel').click(function () {
        Info($(this).data('info'));
    });
});

Elsewhere in your JavaScript you can always access the data-info attribute using something like this:

myLink.data('info');

In a Chrome extension

You could temporarily replace the Info function with something that captures the values that is passed to it:

function getInfoParam(func) {
    var originalInfo, result;

    originalInfo = window.Info;
    window.Info = function (arg) {
        result = arg;
    }
    func();
    window.Info = originalInfo;

    return result;
}

Alternatively, you can use a regular expression to find a quoted string:

function getInfoParam(func) {
    var match;
    match = func.toString().match(/"([^"]*)"/);
    if (match) {
        return match[1];
    }
}

This implementation is limited: It won't work for strings that contain escaped quotes (e.g. "foo\"bar"), or strings that are delimited by single quotes (e.g. 'foobar').

Whatever you do will be vulnerable to changes in the host website, and therefore somewhat fragile.

georgebrock
  • 28,393
  • 13
  • 77
  • 72
  • That approach I can do if I was the webmaster, now I'm trying with .tostring() and .replace() to clean the object and is working (somehow). I would like to don't abuse it (I call replace 4 times) so any other solution is appreciated. – Braiam Sep 02 '12 at 19:33
  • It wasn't clear from your question that you're writing a Chrome extension (I didn't notice the tag). I've updated my answer with some alternative techniques. – georgebrock Sep 03 '12 at 06:51