Edit javascript in IE

B

bengee

Hi

Does anyone know of a way to edit "live" javascript (that i have no
control over) in IE?

Example, i visit a website and see a webpage in IE containing
Javascript. Can i edit that javascript and get IE to accept any changes
i've done to it?

So what i'm saying really, is that i want to hack the javascript served
to me from a website.

Ben
 
J

Jim Ley

Example, i visit a website and see a webpage in IE containing
Javascript. Can i edit that javascript and get IE to accept any changes
i've done to it?

So what i'm saying really, is that i want to hack the javascript served
to me from a website.

yep, loads of easy ways, easiest for simple changes on a one of basis
is just to use javascript:yourCode;void 0 - the yourCode will execute
in the context of the page, and the void 0 will stop navigation.

Jim.
 
B

bengee

Jim said:
yep, loads of easy ways, easiest for simple changes on a one of basis
is just to use javascript:yourCode;void 0 - the yourCode will execute
in the context of the page, and the void 0 will stop navigation.

I'll give that a try, thanks.

Anyway's to edit the actual code in a text editor or similar?

bengee
 
I

Ivo

Follows a bookmarklet that will a page's generated source (that is: the
source including script tags and anything the script may have contibuted to
the page) in a new window which has
1. a textarea where you can edit the code
2. an button "Apply" that overwrites the page with your edited version.
3. a "Restore" button that will attempt to, eh, restore the old page.

This the code (bookmarklet code should be all on one line):
javascript:na=open('','edit','top=20,width=650,height='+(screen.availHeight-
99)+',directories,menubar,resizable');d7c=document.documentElement.innerHTML
;d7o=d7c;while(d7c.indexOf('<')>-1)d7c=d7c.replace('<','&lt;');na.document.w
rite('<html><head><title>Sourcedit
"'+document.title+'"</title><style>body{margin:0px;border:0px;font:m
essagebox;background:AppWorkspace}input{font:messagebox;width:60;}textarea{w
idth:100%;}</style><script>d7o=opener.d7o;onresize=r;onload=r;function
r(){f.ta.style.height=document.body.clientHeight-f.ta.offsetTop-1}</script><
/head><body><form name=f><input type=button value=Apply
onclick="opener.document.open();opener.document.write(f.ta.value);opener.doc
ument.close();opener.focus()"><input type=button value=Restore
onclick="f.ta.value=d7o;f.ta.focus()"> Sourcedit <a
href="'+location.href+'">...'+location.href.substring(location.href.length-4
0)+'</a> ('+(b=d7o.split('\n').length)+' lines,
'+Math.round(d7o.length/b*10)/10+' cpl)<br><textarea
id=ta>'+d7c+'</textarea></form></body></html>');na.document.close();na.docum
ent.f.ta.focus();


Follows a bookmarklet that will prompt for a javascript expression. For
example if you know a page uses a global variable called "myVar", simply
type myVar in the prompt and the current value will be returned. Or if you
wish to change a function called "myFunc", enter function myFunc(){/* new
code */} and myFunc will behave in the new way from then on.

This is the code:
javascript:function JSexe(){ if (valJS!='null' &&
valJS!='undefined')strJS=valJS; strJS=prompt('Your JavaScript code or
variable:',strJS); if (strJS!=null && strJS!='' && strJS!='undefined'){
setTimeout('valJS=\'\'+eval(strJS);JSexe()',10); }
else{valJS='';strJS='';}}valJS='';strJS='';JSexe();

HTH
Ivo
www.4umi.com/web/bookmarklet
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
I'll give that a try, thanks.

Anyway's to edit the actual code in a text editor or similar?

For me, View Source shows a copy in Notepad; then use Notepad Save As so
that you can work on the copy in a well-chosen location on disc. To
view it, enter location in browser address bar.

Or use browser Save As, and find it with your preferred editor.
 
L

Lasse Reichstein Nielsen

Dr John Stockton said:
For me, View Source shows a copy in Notepad; then use Notepad Save As so
that you can work on the copy in a well-chosen location on disc. To
view it, enter location in browser address bar.

One of the things I like about Opera, is that view source lets me edit
the source (with, e.g., notepad) directly in the cache. If I make a change
and refresh the page, I see the change in action, while pressing reload
gets the source from the server again.

-L
 
B

bengee

Dr said:
For me, View Source shows a copy in Notepad; then use Notepad Save As so
that you can work on the copy in a well-chosen location on disc. To
view it, enter location in browser address bar.

Or use browser Save As, and find it with your preferred editor.

Doctor

I need to be able to "reload" the javascript back into IE (website only
supports IE :-(

bengee
 
B

bengee

Lasse said:
One of the things I like about Opera, is that view source lets me edit
the source (with, e.g., notepad) directly in the cache. If I make a change
and refresh the page, I see the change in action, while pressing reload
gets the source from the server again.

This sounds like exactly the thing i need, trouble is that the website i
need to access only supports IE :-(

bengee
 
B

bengee

Dr said:
For me, View Source shows a copy in Notepad; then use Notepad Save As so
that you can work on the copy in a well-chosen location on disc. To
view it, enter location in browser address bar.

Duh, i didn't fully read your post John. I'll give that a try :)

bengee
 
T

Tom

bengee said:
Duh, i didn't fully read your post John. I'll give that a try :)

bengee

I 'm sure you missed Ivo's post too as you would certainly have replied had
you tried his code. Absolutely BRILLIANT bookmarklet. I 've been dreaming
about such a thing for years. No need for Notepad or any other application.
Edit the page right there and then in your browser, script, style, anything,
and see the page change almost as you type! The style tags aren't really
necessary I believe, so cut them out if you like. Here is the code again:

This the code (bookmarklet code should be all on one line):
javascript:na=open('','edit','top=20,width=650,height='+(screen.availHeight-
99)+',directories,menubar,resizable');d7c=document.documentElement.innerHTML
;d7o=d7c;while(d7c.indexOf('<')>-1)d7c=d7c.replace('<','&lt;');na.document.w
rite('<html><head><title>Sourcedit
"'+document.title+'"</title><style>body{margin:0px;border:0px;font:m
essagebox;background:AppWorkspace}input{font:messagebox;width:60;}textarea{w
idth:100%;}</style><script>d7o=opener.d7o;onresize=r;onload=r;function
r(){f.ta.style.height=document.body.clientHeight-f.ta.offsetTop-1}</script><
/head><body><form name=f><input type=button value=Apply
onclick="opener.document.open();opener.document.write(f.ta.value);opener.doc
ument.close();opener.focus()"><input type=button value=Restore
onclick="f.ta.value=d7o;f.ta.focus()"> Sourcedit <a
href="'+location.href+'">...'+location.href.substring(location.href.length-4
0)+'</a> ('+(b=d7o.split('\n').length)+' lines,
'+Math.round(d7o.length/b*10)/10+' cpl)<br><textarea
id=ta>'+d7c+'</textarea></form></body></html>');na.document.close();na.docum
ent.f.ta.focus();
 
B

bengee

Tom said:
I 'm sure you missed Ivo's post too as you would certainly have replied had
you tried his code. Absolutely BRILLIANT bookmarklet. <snip>

Tried Ivo's 2 bookmarklets. The 1st (big) one doesn't work. 2nd one ok
though. What version of IE are you using Tom and Ivo?

bengee
 
T

Tom

bengee said:
Tried Ivo's 2 bookmarklets. The 1st (big) one doesn't work. 2nd one ok
though. What version of IE are you using Tom and Ivo?

bengee

I use IE6 on Windows XP. I had to shorten the code to make it work. That is
why I mentioned the style tags. With IE6 's diminished support for
bookmarklets, bookmarklet code should be kept to an *absolute* minimum.
Tom
 
I

Ivo

bengee said:
Tried Ivo's 2 bookmarklets. The 1st (big) one doesn't work. 2nd one ok
though. What version of IE are you using Tom and Ivo?

bengee

There seems to be an error in that code, I 've heard it before, but nobody
's found it yet.
IE5.5 on WinME has no problems, as far as my machine goes.
Here is a stripped down version:

javascript:
d7c=document.documentElement.outerHTML;
while(d7c.indexOf('<')+1)
d7c=d7c.replace('<','&lt;');
na=open('','eW','directories,menubar,resizable');
na.document.write('<form name=f><input type=button value=Apply
onclick="with(opener.document){open();write(document.f.ta.value);close();}op
ener.focus()"><br><textarea id=ta cols=99
rows=32>'+d7c+'</textarea></form>');
na.document.close();
na.document.f.ta.focus();

But no editor can beat the wysiwygs of this one:

javascript:
if(document.body.contentEditable != 'true')
void(document.body.contentEditable = 'true');
else void(document.body.contentEditable = 'false');

A problem not solved by the suggestions sofar in this thread involves
external js and css. A bookmarklet to pop up as many Notepads as there are
external scripts and styles on a page, plus one for the page source itself,
is as follows:

javascript:
var r=new Image(),s=document.getElementsByTagName("SCRIPT");
for(var n=0;n<s.length;n++)
if((r.src=s[n].src))
open("view-source:"+r.src);
s=document.styleSheets;
for(var n=0;n<s.length;n++)
if((r.src=s[n].src))
open("view-source:"+r.src);
open("view-source:"+location.href);
void('Free Mumia Abu-Jamal!');

And all on one line.
www.4umi.com/bookmarklet/edit.htm is an online, well the url sais it all.
One click of the mouse will compress the code to a single line, or format it
with indentation as shown above, after some basic validation. Some more
advanced functions, such as find-n-replace, are still in beta, and IE only
until other browsers learn proper textrange handling. I need to break the
problems down, then I can post some code here for the whoever to chew on.
I wish to claim none of this code as my own, everything is cut-n-pasted
together.
Ivo
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
Follows a bookmarklet that will a page's generated source (that is: the
source including script tags and anything the script may have contibuted to
the page) in a new window which has
1. a textarea where you can edit the code
2. an button "Apply" that overwrites the page with your edited version.
3. a "Restore" button that will attempt to, eh, restore the old page.


ISTM that the technique, while convenient and ingenious, may be
vulnerable to loss.

If the revised page is saved to disc, then that is as safe as anything
can be on such a computer (still vulnerable to burglary, fire, ...).

But it seems that editing-in-place will be vulnerable to anything that
crashes or locks up the browser, including errors in editing.

My browser instance was locked, for example, by the posted code of the
"decompressor", perhaps because I'd failed to spot a line-wrap. Other
instances kept running, but all died when I tried to kill the locked
one.
 
I

Ivo

Dr John Stockton said:
JRS: In article <[email protected]>, seen in


ISTM that the technique, while convenient and ingenious, may be
vulnerable to loss.
(...)
My browser instance was locked, for example, by the posted code of the
"decompressor", perhaps because I'd failed to spot a line-wrap. Other
instances kept running, but all died when I tried to kill the locked
one.
© John Stockton, Surrey, UK. [email protected] Turnpike v4.00 IE
4 ©

Absolutely. Sometimes I want to backup a testpage but not create a whole new
file for it. Remembering to remove that later on is just too much work.
The clipboard is then a most convenient place to temporariliy store the
original code. Not the safest place, and when the test is powerful enough to
crash the computer, things get lost, yes.
Methinks that the weak points of the ideas that have been put forward, are
clear enough, but that the original request aimed for something as simple as
poss, with no intent to save the edited file or opening of other programs.
Sorry, which decompressor code?
Ivo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,098
Messages
2,570,625
Members
47,236
Latest member
EverestNero

Latest Threads

Top