-2

Possible Duplicate:
Server side browser that can execute JavaScript
Execute javascript in PHP

How can I parse HTML that includes JavaScript code, preferably with PHP script if possible.

As an example:

<a href="javascript:link(10, true);">link</a>

should be replaced by the appropriate value the JavaScript function returns, e.g.

<a href="http://www.example.com">link</a>

A more complex example would be a saved facebook html page which is littered with loads of javascript code.

Summarized: Return a DOM for a page with html+javascript

25
  • How about simplehtmldom.sourceforge.net ? Commented Jul 29, 2012 at 17:06
  • @Adnan that doesn't interpret JavaScript as the OP wants Commented Jul 29, 2012 at 17:08
  • 1
    I'm pretty sure that is not possible in the general case. What if for example the link function uses the user's mouse coordinates to decide what link to make? Commented Jul 29, 2012 at 17:09
  • 1
    So, you want to parse the HTML string with PHP, but then additionally interpret the JavaScript code? Do you have a JavaScript interpreter on your web-server? Commented Jul 29, 2012 at 17:11
  • 1
    and Server side browser that can execute JavaScript Commented Jul 29, 2012 at 17:13

3 Answers 3

1

You could just give this link an ID. Not that this solution is javascript, jQuery.

So give the link an Id, or class.

$('.link').each(function() {
    var functionName = 'link';
    var start = $(this).attr('href');
    remove = start.replace('javascript:', ''), 
        get = remove.replace(new RegExp('^'+functionName+'\(((.+\,?)+)\)\;?', 'g'), function(a, b, c) {
        return c.replace(/[()']/g,'')
    }), args = get.split(",");

    //read settings
    var firstArgument = args[0];
    $(this).attr('href', firstArgument)
});​

Please note this is just an example.

Usage:

​<a class="link" href="javascript:link('http://facebook.com')"​>Hi</a>​​​​​​​​​​​​​​​​​​

That would make the actual link http://facebook.com.

Adding new arguments this way is difficult though and its not really professional. But this should do what you want, I just didn't know what your link function actually doesnt so I didnt add the argument with the boolean. Of course this could get far more complex and you could write a function that could do this too but I just wrote this for your really quick.

Check out the example.

Sign up to request clarification or add additional context in comments.

8 Comments

This answer is useless for links built dynamically from functions or through even more complex structures such as JQuery. Also it only preg_matches the link out it does not actually execute the JS code to get the final link as such if link() didn't take any params this code would reutrn nothing as shown in the users own example: <a href="javascript:link(10, true);">link</a>
It gets the final link. Maybe not like the OP did it but that because I didnt know what their link function actually does. This was just an example and you can make it work for dynamically added links.
And that is the #1 problem, in order to understand the JS must be evaled to get the variable content return.
Also why would facebook or someone put a function into the href attribute of a link with it's param being that of the target link? One reason could be for outbound proxies to protect users from malicious content (`l.php' in this case for fb) but then this function still wouldn't return that proxied link but instead the initial potentially unsafe param that was entered. That's why I don't really see how this answer would work.
Now to take the example the OP posted: jsfiddle.net/Sammaye/H3qNW
|
0

You can't. When clicked, javascript: URIs just call the function and let it do whatever it wants (which MIGHT include navigating somewhere); they don't expect or use a return value. In many use cases, the function may not cause any navigation at all.

4 Comments

Hey, it's just an example, my point is to render javascript as html
There's a world of complexity in "render javascript as html" that I don't believe you comprehend.
What @Michael says. This is a huge task.
Yes, this would be far more complex then you think.
0

DomDocument can be used to parse HTML in PHP including JS: http://php.net/manual/en/class.domdocument.php

You can "render" the JS with the HTML by merely echoing out the output of a cURL or wget (or whatever you use) without escaping the HTML characters. For external JS you are going to need to build a crawler which will crawl the DomDocument script tags and fetch the appropiate URL and load it into a position that is accessible unless you use it directly from their servers but I don't think they will be too happy about that.

Edit: My new answer after some comments is: no

12 Comments

@Adnan Where? I am unsure what you mean
I think what @Adnan means is 'since when does DomDocument parse JS?'.
JS is basically the textual content of a HTML tag/attribute as such (the last time I used it) script tags were accessible from DomDocument. I mean he just wants the JS with the HTML from what I see in his question. He does not actually want to parse the JS in a manner which will allow syntax highlighting...
Crazy might be a bit strong here, but my guess is he doesn't fully understand the complexity and context required to interpret those links.
@Vatev Indeed he does not, he would need integrate a JS engine directly since the JS can be executed in so many ways the only way is to hook into the actual link redirect event that a browser normally fires beore it moves to the next link which means he would need to build his own eval sandbox into JS or possibly PHP. To make it all work is insane, hence why Google doesn't do it, well or at all...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.