Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

COM_L Inject javascript


  • Please log in to reply
12 replies to this topic
Visioneer
  • Members
  • 287 posts
  • Last active: Sep 17 2017 10:07 PM
  • Joined: 19 Nov 2007

Hi,

 

Using COM_L ahk_l unicode, how do I inject javascript.

 

I would like a simpler answer which only injects into a freshly created Pwb IE webpage.

(eg: only has the one tab)

 

And the more complex answer for an existing webpage.

 

Thanks

 



TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006

...a simpler answer which only injects into a freshly created Pwb IE webpage../..existing webpage.

This should answer both questions
url = http://blank.org/

String := "<h1>This is a freshly injected pwb IE webpage.<br />It was made in AHk: " A_AhkVersion "</h1>"

pwb := ComObjCreate("InternetExplorer.Application"), pwb.Visible := 1
pwb.Navigate(url), Wait(pwb,300)

pwb.document.getElementsByTagName("Body")[0].innerHTML := String

ExitApp

Return
Wait(obj, int="") {
    While(!Instr(rs,4)||StrLen(rs)<int) 
        rs.=obj.ReadyState
}

Posted Image

don't duplicate, iterate!


Visioneer
  • Members
  • 287 posts
  • Last active: Sep 17 2017 10:07 PM
  • Joined: 19 Nov 2007
Hi TLM,
 
Thanks for the reply. Sorry for my late reply. I got involved with the ie_inject function, but it uses
the execscript function which will be discontinued by Microsoft with IE 11+, I hear.
 
BTW: I am using AHK_L version 1.1.13.00 Unicode. jethrow does not like the phrase COM_L so I
have stopped using it.
 
Your idea is very interesting but I don't see how a javascript would get executed after it is appended.
 
pwb.document.getElementsByTagName("Body")[0].innerHTML := String
 
I have tried this and many other variations.  Note the ` escaping for % characters to avoid errors for String =
String =
(
<script>
function Go()
{
document.body.style.overFlow = "hidden";
var img1 = document.getElementsByTagName("img").item(0);
var sw = screen.width;
var sh = screen.height;
var iw = img1.width;
var ih = img1.height;
var ir = iw/ih;
var sr= sw/sh;
if(sr <= ir)
img1.style.width = "100`%";
else
img1.style.height = "100`%";
}
</script>
)
pwb.document.getElementsByTagName("Body")[0].innerHTML .= String
The web page URL is any URL that goes to a single jpg file directly.
 
This works with the IE_inject() I currently use, but it needs "execsxcript".
String =
(
document.body.style.overFlow = "hidden";
var img1 = document.getElementsByTagName("img").item(0);
var sw = screen.width;
var sh = screen.height;
var iw = img1.width;
var ih = img1.height;
var ir = iw/ih;
var sr= sw/sh;
if(sr <= ir)
img1.style.width = "100`%";
else
img1.style.height = "100`%";
)
IE_InjectJS(WinExist("ahk_class IEFrame"), String)
It simply turns off scrollbars and displays the jpg photo fullscreen,
as large as is possible without cutting off any part of the photo.
It is fullscreen because I use pwb.FullScreen := True  when I open
the pwb window.
 
So, can I make the script run on the initial inject, or can I make it run
with a 2nd inject maybe?
 
The ":=" that you showed just makes the jpg photo diapppear, but ".=" seems
to add it after the photo, I think. At least "text" appears after the photo.
 
Also, since the pwb web page is fullscreen I can't just put it into the address bar.
So the question is: How can I inject AND run some javascript into a pwb fullscreen
web page that only contains a single jpg or graphic file, without using "execscript".
 
Thanks
 
p.s. Microsoft said they were recommending to use "eval" instead of "execscript" in the future.

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009

If you sift out the massive amount of extra useless discussion, there's some decent code in this thread: COM_L Inject javascript



Visioneer
  • Members
  • 287 posts
  • Last active: Sep 17 2017 10:07 PM
  • Joined: 19 Nov 2007
Hey Jethrow,
 

BTW: The link for MS says:
[execScript is no longer supported. Starting with Internet Explorer 11 Preview, use eval. For info, see Compatibility changes.]


The "useless discussion" was mostly you trying to obscure the problem that AHK is facing. Without execscript,
it may be impossible to inject AND RUN any serious javascript in IE 11+ which is rapidly gaining
market share. I do not have IE 11+ yet, but that is what you get now with all new PC's via W8+. It is
also going to be automatically updated to IE 11 for W7 users which means most users pretty soon.
It also means that 1000's of ahk scripts will no longer work.

I see the Navigate running "javascript" like: alert("hello world");, but executing javascript contained
in a var without execscript? I can't find any examples. (And you know I do not mean a complete ahk
translation every time you want to use "existing" javascript).

I do not have IE 11 yet myself. I use IE9 on W7 mostly. With that setup, I can't get the "eval"
to do anything. Maybe it will do something in IE 11+, but from what I have read, it is a big problem
for all the other languages too.

For years everyone has been told that "eval" is evil, bad, etc. It looks to me like MS has had this
javascript injection curtailment in mind for years. Pretty ironic.

I think we will be needing ways to detect IE 11+ or the absence of execscript, and hopefully find
a work around for eval. If the "eval" possible works in IE 11+ this could be done. Hopefully some
folks out there are using IE 11+ and v1.1 and can try it out.

So let us allow this thread to provide IE verson detection, execscript/eval detection, and ways to
survive MS latest attempt to kill javascript injection and dumb down the software world.

Maybe an update to ahk will be possible.

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009

... executing javascript contained in a var without execscript? I can't find any examples ...

 
Here's an example:
var := "foo='bar'"
window.eval(var)
This is of course assuming that window contains a valid window object - but more importantly that you have the correct credentials for that window object. In other words, if you access the window object via HTML Dom navigation, this won't work. Here is a working example from scratch:
IID := "{332C4427-26CB-11D0-B483-00C04FD90119}" ; IID_IWebBrowserApp

wb := ComObjCreate("InternetExplorer.Application")
wb.Navigate("www.google.com")
wb.Visible := true
while wb.busy
	sleep 10
window := ComObj(9,ComObjQuery(wb,IID,IID),1)

var := "foo='bar'"
window.eval(var)
MsgBox % window.foo

Jethrow does not like the phrase COM_L so I have stopped using it.

Should I assume you mean someone else with a capital J? I ask because I never showed distaste for the phrase COM_L - when it's being used correctly. However, if you are still using the COM(_L) library, I must inform you that it's been deprecated for years now. Native COM was implemented into AutoHotkey v1.1+ (previously AutoHotkey_L) & is highly recommended over using the COM(_L) library. Moreover, attempting to mix the COM(_L) library with Native COM could cause quite a bit of confusion & should be avoided.

Visioneer
  • Members
  • 287 posts
  • Last active: Sep 17 2017 10:07 PM
  • Joined: 19 Nov 2007

BTW: I am using AHK_L version 1.1.13.00 Unicode. jethrow does not like the phrase COM_L so I
have stopped using it.

I have corrected your moniker. Sorry.

jethrow, I love what you did. It does let me get a var from a web page, even a pre-existant var
from pages that have one.

However it does not execute my javascript for me. Please try something using my simple js script.

Note that using your thing with:
window.execscript(String)
does work and runs the javascript shown above.

Also I use this bookmarklet approach to see what innerHTML is in the page.
Script = javascript: alert(document.documentElement.outerHTML); void(0);
ControlSetText, Edit1, %Script%, ahk_class IEFrame
ControlFocus, Edit1, ahk_class IEFrame
send, {Enter}
Although I can actually use this bookmarklet approach to run shorter js scripts, I can't use it
for longer scripts like I can with execscript.
[code=auto:0]
ControlSetText, Edit1, %Script%, ahk_class IEFrame
ControlFocus, Edit1, ahk_class IEFrame
send, {Enter}[/code

I have also tried countless variations of TLM's approach above with no luck.
Maybe 1st thing could inject the javascript and a 2nd thing could run it?

Ultimately, I would like to see a function that would let people plug it in
wherever they have used "execscript" in their code. Like for example the old
ahklearner's IE_Inject function you made for AHK_L and referred to earlier in
this thread.

I hope that some folks out there with IE 11+ can test out the final results of
this thread.

Thanks


p.s. What is the latest on IE version and Windows version detection?

p.p.s.
Here is a link to a wide landscape photo jpg file.
http://www.desktopic...ckgrounds-2.jpg
Here is a link to a tall portrait photo.
http://photoslaves.c...r-dali-1954.jpg

My javascript above displays either photo to fill the screen as big as possible without
cutting off any part of photo. I have a much longer version that does lots of other tricks.
It is way too long for the bookmarklet approach.

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009

However it does not execute my javascript for me. Please try something using my simple js script.

Note that using your thing with:
window.execscript(String)
does work and runs the javascript shown above.


The following does the same using eval or execScript on Win7 64bit using IE 11:
IID := "{332C4427-26CB-11D0-B483-00C04FD90119}" ; IID_IWebBrowserApp
String =
(
document.body.style.overFlow = "hidden";
var img1 = document.getElementsByTagName("img").item(0);
var sw = screen.width;
var sh = screen.height;
var iw = img1.width;
var ih = img1.height;
var ir = iw/ih;
var sr= sw/sh;
if(sr <= ir)
img1.style.width = "100`%";
else
img1.style.height = "100`%";
)

wb := ComObjCreate("InternetExplorer.Application")
wb.Navigate("http://www.desktopict.com/wp-content/uploads/2014/02/landscape-backgrounds-2.jpg")
wb.Visible := true
while wb.busy
	sleep 10
window := ComObj(9,ComObjQuery(wb,IID,IID),1)
MsgBox Click OK to proceed ...
window.eval(String)
;~ window.execScript(String) 


Visioneer
  • Members
  • 287 posts
  • Last active: Sep 17 2017 10:07 PM
  • Joined: 19 Nov 2007
Hi jethrow,

The 2 jpg file links I sent you were not the best examples. They are large
and have this click to enlarge property. They might make it appear that
something is happening when it is not.

The scrollbars should disappear and the small photos should fill the screen
by either the width or the height.
Check out the scrollbars carefully to see if it is actually working.

small plain wide photo
;fURL = http://upload.wikime...barry_stamp.JPG
small plain tall photo
;fURL = http://upload.wikime...uring_Dante.jpg
wb := ComObjCreate("InternetExplorer.Application")
wb.FullScreen := True
wb.AddressBar := False
wb.ToolBar := False
wb.MenuBar := False
wb.StatusBar := False
wb.Visible := true

;wb.Navigate("[url=http://www.desktopict.com/wp-content/uploads/2014/02/landscape-backgrounds-2.jpg]http://www.desktopict.com/wp-content/uploads/2014/02/landscape-backgrounds-2.jpg")[/url]
wb.Navigate(fURL)
(use Alt+F4) to close the fullscreen in this limited example. (I usually use an Esc hotkey to close).

For me, using IE 9/7 W7 v1.1+ unicode, the execscript works, but the eval does not.

Do you think it's possible that IE 11 eval behaves differently than IE 9/7 eval?

Thanks

jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009

How about something simple like:

IID := "{332C4427-26CB-11D0-B483-00C04FD90119}" ; IID_IHTMLWindow2
js  := "document.getElementsByTagName('img')[0].src = 'http://www.autohotkey.com/static/ahk_logo_ipb.png';"
wb  := ComObjCreate("InternetExplorer.Application")
wb.Navigate("http://upload.wikimedia.org/wikipedia/commons/e/ee/US_stamp_honouring_Dante.jpg")
wb.Visible := true
while wb.busy
   sleep 10
window := ComObj(9,ComObjQuery(wb,IID,IID),1)
MsgBox Change Image
window.eval(js)

Note that in this example, the eval method works on IE 11, but won't work on IE 8.



Visioneer
  • Members
  • 287 posts
  • Last active: Sep 17 2017 10:07 PM
  • Joined: 19 Nov 2007

Nor IE 9, IE 9 in IE 7 compatibility mode.

 

Can someone try in IE 10?

 

The execscript does work btw.

 

Note that if I change the

wb.Navigate(http://upload.wikime...uring_Dante.jpg)
to

wb.Navigate("http://ahkscript.org/")

eval does work.

 

So that means that there is different eval functionality in IE9. It won't work I guess on a photo only webpage?

 

So I guess people will have to check the IE version and use execscript for  11- and eval for 11+

 

There are many changes to IE 11 and yet more to IE12... There should be a thread dedicated to

these changes.  I have a version function I use which can get the version of 1, 2 and full version

numbers for example.

 

The bookmarklet approach is OK for small javascripts if we can count on COM's

ComObjCreate("InternetExplorer.Application")
browsers always having Edit1 be the address bar, whatever toolbars are installed.

I know that the google toolbar changed that in regular IE windows. But maybe

the COM windows are always Edit1, is that the case?  I don't like the bookmarklet

approach in general because you never know the js length limit, and maybe apps

would be used on regular non ahk generated windows anyway.

 

Here are some IE11 changes. Although others are planned.

http://msdn.microsof...5(v=vs.85).aspx

 

Another particular concern for me is:

script.onreadystatechange and script.readyState

script.onload

Are they messing with things like:

while wb.busy
   sleep 10
and other waits used by many AHK apps?

 

Thanks for your great work jethrow.



tsaiid
  • Members
  • 1 posts
  • Last active: Jan 05 2017 09:57 AM
  • Joined: 18 Feb 2014

 
This is of course assuming that window contains a valid window object - but more importantly that you have the correct credentials for that window object. In other words, if you access the window object via HTML Dom navigation, this won't work.

 

 

I used AHK_L 1.1.15, Windows 7 64-bit, IE11. The WBGet() is from (http://www.autohotke...t-com-tutorial/).

I want to inject some js into a pre-existed IE window.

wb := WBGet()
window := wb.document.parentWindow

var := "foo='bar'"
window.eval(var)

However, error occurred and it seemed no eval method in the window object. If the environment changed to WinXP, IE8, it worked. Is this because I don't have correct credentials? How can I have it?



jethrow
  • Moderators
  • 2854 posts
  • Last active: May 17 2017 01:57 AM
  • Joined: 24 May 2009

I see you used the document & parentWindow methods to get the window object. Did you try using the approach from my previous post ?

IID := "{332C4427-26CB-11D0-B483-00C04FD90119}" ; IID_IHTMLWindow2
window := ComObj(9,ComObjQuery(wb,IID,IID),1)