HighDots Forums  

Re: How to make injected js execute?

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: How to make injected js execute? in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #11  
Old   
Randy Webb
 
Posts: n/a

Default Re: How to make injected js execute? - 08-20-2007 , 09:33 PM






Peter Michaux said the following on 8/20/2007 8:43 PM:
Quote:
On Aug 20, 5:22 pm, Randy Webb <HikksNotAtH... (AT) aol (DOT) com> wrote:
Peter Michaux said the following on 8/20/2007 11:55 AM:
URL:http://forkjavascript.org/mutate/docs
What my script does it scan through the HTML that is about to be
inserted and pluck out all the script bits. It inserts the other HTML
and then process the JavaScript bits. Instead of dynamic script
insertion, as Randy has investigated, I just eval() the script bits.
This is more cross-browser but has scope implications.
I have a script somewhere that lets you simply insert it via innerHTML
and then reads the script blocks back out and executes them. Is that
what yours does? (I haven't looked at it).

I extract the scripts from the HTML before inserting the HTML into the
page. That way if some browser starts to automatically evaluate
scripts when they are inserted by innerHTML I'm not screwed. There
already is one old browser out there that does this: NN6? Then after
inserting the HTML, I eval() the script bits in order. There are pros
and cons for just about every decisions in dealing with this stuff. I
find the system I built easy to use.
NN6.1, NN6.2, iCab3 and IE5.2/Mac execute scripts inserted via
innerHMTL. Detecting that behavior is trivial though. Use the onload
event, insert something like this via innerHTML:

<script type="text/javascript">
someBooleanVariable = false;
</script>

And have this already in the page:
<script type="text/javascript">
someBooleanVariable = true;
</script>

And then have your function check that variable:

function doSomethingWithThatAwfulCode(){
if(someBooleanVariable){return}
}

Then, if they get executed previously you know not to fool with them
again, you simply return out of the function.

Quote:
There is a bit more too it then that. My script takes into account
problems with inserting table rows or tbody elements by "parsing" the
html first in the appropriate surrounding element. There are
assumptions about a well formed html fragment. blah blah blah. That
script was actually a fun project.
I have never been one to try to dynamically create tables on the fly.
Most of my interest in it is the fact that IE gets it right and FF gets
it wrong. The personal page I was working on when I first started
playing with HikkScript (JS on the fly) used tables but I haven't fooled
with it in about 5 years or so. Can you imagine trying to reverse a
script that document.writes embedded tables though?

Note: I have decided to start calling this HikkScript just for the sake
of it and make it easier to find in the archives. It has a certain
"personal ring" to it

Quote:
If you are not in charge of the script bits and want to use script
insertion you could combine my library and one of the solutions posted
in the other c.l.j thread about script insertion.
I have yet to see a solution that handles document.write but I have an
idea in mind for it.

You are obsessed!
That's what they tell me. A "This attitude plus FAQ mantainer: a true
humanitarian!" is what one poster here said about me when I said I
wanted to be ahead of the game on this one (You can find that in the
createTextNode IE7 thread).

Quote:
Just curious but when is it that you are wanting to
do script insertion with totally random scripts you haven't written
yourself or written by a coworker?
Personally? I don't. It comes up mostly from ill-fated "Ajax sites"
where people don't re-do the back end to make it ajax friendly and just
retrieve complete pages and want to insert it. Then they can say "Yeah,
we use AJAX on our site" without taking advantage of the main advantage
of AJAX.

Quote:
Just academic interest?
Mostly, as anybody that is trying to insert HTML with document.writes in
it has more problems than trying to get the document.write to work right
as they need to change the back end to get rid of the document.write
statements.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Reply With Quote
  #12  
Old   
Randy Webb
 
Posts: n/a

Default Re: How to make injected js execute? - 08-20-2007 , 11:52 PM






Randy Webb said the following on 8/20/2007 8:46 PM:
Quote:
Jon Maz said the following on 8/20/2007 5:20 AM:
Hi Peter,

Thanks for that - I had no idea this would be a complex issue. A
couple of observations:

1. for (possibly demented) reasons of my own it's actually an
arbitrary mix of js and html that I want to inject into the page, so
of the 5 methods offered in the other thread:

* Change Source- attempts to change the .src property of a script
element.
* Change innerHMTL - inserts a script string via innerHTML.
* createElement - uses createElement to create a script block with a
.src attribute.
* Change .text - changes the .text property of a script element.
* createTextNode - creates the text part of a script element using
createTextNode

... I think only the .innerHTML one is an option for me.

The thread that Peter sent you to isn't the best thread for what you are
trying to do. There is another one in the archives if I can find it.

URL:
http://groups.google.com/group/comp....HTMLFragme nt


The name of the function that was last written for what you are doing is
named loadHTMLFragment and looks like this:
I have revised that function to accommodate NN6.1/NN6.2Win and hopefully
it will accommodate the innerHTML mac browsers (iCab and IE). If anyone
could test it on a non-windows browser I would be grateful. Added was
the innerHTML testing so that browsers that execute innerHTML scripts
wouldn't execute them twice.

var innerHTMLFailed=true;
window.onload = checkIt;

function checkIt(){
document.getElementById('myDiv').innerHTML = '<script
type="text/javascript">var innerHTMLFailed = false;<\/script>'
}
function loadHTMLFragment(elemId, HTMLFragment)
{
if (document &&
document.getElementById &&
document.getElementById(elemId) &&
document.createElement &&
document.appendChild &&
document.getElementsByTagName &&
{
var el = document.getElementById(elemId);
el.innerHTML = "&nbsp;" + HTMLFragment;
//The &nbsp; is a hack to cause IE to process the
//script elements if the first node in the
//HTMLFragment is a script element.
if(innerHTMLFailed)
{
var d =el.getElementsByTagName('script');
var t = d.length;
for (var x=0;x<t;x++)
{
var newScript = document.createElement('script');
newScript.type = "text/javascript";
newScript.text = d[x].text;
el.appendChild(newScript);
}
for (var y=0;y<t;y++)
{
el.removeChild(el.getElementsByTagName("script")[y]);
}
}
}
}

Next step is a test of the .text attribute since I know of browsers that
do not support setting the .text property of a newScript element.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Reply With Quote
  #13  
Old   
Thomas M. Farrelly
 
Posts: n/a

Default Re: How to make injected js execute? - 08-21-2007 , 03:45 AM



On Mon, 20 Aug 2007 22:27:50 +0000, Peter Michaux wrote:

Quote:
On Aug 20, 2:20 pm, "Thomas M. Farrelly" <tho... (AT) metatools (DOT) org> wrote:
[...]
A function's apply() will set the scope the "this" keyword when the
function runs. There is more to the script insertion problem then just
resolving "this". Here is an example where the script to be inserted
is "var foo=1;"

Realizing that :-)

[...]
Quote:
Peter

Reply With Quote
  #14  
Old   
Jon Maz
 
Posts: n/a

Default Re: How to make injected js execute? - 08-21-2007 , 06:23 AM



<quote>
You are obsessed! Just curious but when is it that you are wanting to
do script insertion with totally random scripts you haven't written
yourself or written by a coworker? Just academic interest?
</quote>

For myself I am trying to write code that asynchronously injects adverts
into a web page. You'll notice across the web millions of sites whose pages
might be quite fast-loading in themselves, but with 3 or 4 massive delays as
the page goes to ads.domain.com to fetch the ad code, which
characteristically is a mix of javascript and html, and is generated by 3rd
party software.

I want this external data injected in the background, after the main page
has finished rendering, for an improved user experience. Make sense?

JON



Reply With Quote
  #15  
Old   
Jon Maz
 
Posts: n/a

Default Re: How to make injected js execute? - 08-21-2007 , 06:25 AM



Hi Peter,

Currently http://forkjavascript.org/mutate/docs is down. A temporary issue?

JON



Reply With Quote
  #16  
Old   
Peter Michaux
 
Posts: n/a

Default Re: How to make injected js execute? - 08-21-2007 , 07:33 AM



On Aug 21, 3:25 am, "Jon Maz" <jon... (AT) surfeu (DOT) deno.spam> wrote:
Quote:
Hi Peter,

Currentlyhttp://forkjavascript.org/mutate/docsis down. A temporary issue?

Unfortunately it is down. I'm trying to get my hosting company to tell
me what's up. Hopefully it will be back up today.

Peter



Reply With Quote
  #17  
Old   
Peter Michaux
 
Posts: n/a

Default Re: How to make injected js execute? - 08-21-2007 , 07:37 AM



On Aug 21, 3:23 am, "Jon Maz" <jon... (AT) surfeu (DOT) deno.spam> wrote:
Quote:
quote
You are obsessed! Just curious but when is it that you are wanting to
do script insertion with totally random scripts you haven't written
yourself or written by a coworker? Just academic interest?
/quote

For myself I am trying to write code that asynchronously injects adverts
into a web page. You'll notice across the web millions of sites whose pages
might be quite fast-loading in themselves, but with 3 or 4 massive delays as
the page goes to ads.domain.com to fetch the ad code, which
characteristically is a mix of javascript and html, and is generated by 3rd
party software.

I want this external data injected in the background, after the main page
has finished rendering, for an improved user experience. Make sense?
What about just inserting an iframe into the page and setting the src
attribute to a URL that produces the ads? Because of iframes, it isn't
a valid approach for HTML strict pages but is on HTML transitional.

Peter



Reply With Quote
  #18  
Old   
Jon Maz
 
Posts: n/a

Default Re: How to make injected js execute? - 08-21-2007 , 09:38 AM



Hi Peter,

<quote>
What about just inserting an iframe into the page and setting the src
attribute to a URL that produces the ads? Because of iframes, it isn't
a valid approach for HTML strict pages but is on HTML transitional.
</quote>

That's probably where I'll end up. For reasons I can't remember now I
started trying to do this the .js way, and I'm stupidly determined to try to
get it to work.

Though in fact I am going for HTML strict too <sigh>.

JON



Reply With Quote
  #19  
Old   
Peter Michaux
 
Posts: n/a

Default Re: How to make injected js execute? - 08-21-2007 , 11:18 AM



On Aug 21, 6:38 am, "Jon Maz" <jon... (AT) surfeu (DOT) deno.spam> wrote:
Quote:
Hi Peter,

quote
What about just inserting an iframe into the page and setting the src
attribute to a URL that produces the ads? Because of iframes, it isn't
a valid approach for HTML strict pages but is on HTML transitional.
/quote

That's probably where I'll end up. For reasons I can't remember now I
started trying to do this the .js way, and I'm stupidly determined to try to
get it to work.

Though in fact I am going for HTML strict too <sigh>.
I tried that for a while. Now I think HTML transitional is the only
useful doctype. For IE6 if you want to do overlays you need the
"iframe shim hack". If you want to do background file uploads that
look like XMLHttpRequest to the user then you need hidden iframes.

If you will be allowing arbitrary people to inject arbitrary HTML in
your page there is a better chance things will work out well if you
are using the more forgiving transitional doctype.

Peter



Reply With Quote
  #20  
Old   
Dr J R Stockton
 
Posts: n/a

Default Re: How to make injected js execute? - 08-21-2007 , 02:52 PM



In comp.lang.javascript message <faeecf$adv$1 (AT) aioe (DOT) org>, Tue, 21 Aug
2007 11:23:40, Jon Maz <jonmaz (AT) surfeu (DOT) deno.spam> posted:
Quote:
For myself I am trying to write code that asynchronously injects adverts
into a web page. You'll notice across the web millions of sites whose pages
might be quite fast-loading in themselves, but with 3 or 4 massive delays as
the page goes to ads.domain.com to fetch the ad code, which
characteristically is a mix of javascript and html, and is generated by 3rd
party software.

I want this external data injected in the background, after the main page
has finished rendering, for an improved user experience. Make sense?
No. That's only what the readers want.

Those who pay for the site want the users to have nothing better to do
while waiting for the wanted part than to read the adverts.

But I use small windows, so that the adverts tend not to show and I can
use another window while waiting for good stuff to appear.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.