You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to replicated the selenium example but using electron only.
I am wondering how to inject jQuery and then set the query to "nice cats" and printout the search results.
I need jQuery to be injected first and then set the query using $("[name='q']")[0].value = "nice cats";
I can open google using the below app.js code.
varapp=require('app')varBrowserWindow=require('browser-window')varipc=require('ipc')app.on('ready',function(){varmainWindow=newBrowserWindow({width: 800,height: 600});mainWindow.loadUrl('http://www.google.com');mainWindow.openDevTools();mainWindow.on('closed',function(){console.log("The page has been closed");mainWindow=null;});mainWindow.webContents.on('did-finish-load',function(){console.log(mainWindow.webContents.getUrl());});});
I want to navigate to the next page using $.click and then print out the next results.
I'd probably do it this way. Instead of loading Google in the mainWindow using loadUrl I'd instead create a basic HTML page that contains a webview that loads Google. I'd then create a small JS file to load in the webview preload. This script would require jQuery like this:
Okay so disregard what I said above. It doesn't appear you can inject jQuery like that in a webview preload script. Try this, this works!
index.html:
<!DOCTYPE html><html><head><title>jQuery injection into webview preload</title></head><bodystyle="overflow:hidden;"><webviewid="webview" preload="./manipulateGoogle.js" src="http://google.com" style="position:absolute;width:100%;height:100%;"></webview><script>// need to have a script tag make webview work even if you don't plan to use it...</script></body></html>
webview preload script (in this example name it manipulateGoogle.js)
// injection technique borrowed from http://stackoverflow.com/questions/840240/injecting-jquery-into-a-page-fails-when-using-google-ajax-libraries-apiwindow.onload=function(){varscript=document.createElement("script");script.src="https://code.jquery.com/jquery-2.1.4.min.js";script.onload=script.onreadystatechange=function(){$(document).ready(function(){$("#lst-ib").val("Hello, World!");});};document.body.appendChild(script);};
I've only briefly used the in-built IPC API so can't really comment much on it. I've also used Socket IO as a "IPC" mechanism and found it to work rather nicely. You could probably use other APIs to communicate back and forth but I haven't really explored it because Socket IO worked well for me in this regard.
Going back to your manipulateGoogle.js example which now after further investigation I understand whats happening. However, most of my page elements are created and built using AJAX requests which means there's nothing for the jquery selectors to find until the page is built.
Appreciate, this is more of a jquery question (maybe), but is there a way to wait until the subsequent page build completes before firing $(document).ready ?
(I haven't tried this but...) If you have control over the code that you are loading (eg you can edit it) why not set up a callback in your webview preload and then when your page gets done loading all it's ajax stuff then call the callback from the code loaded in the webview to notify it that your page is done doing everything so you can then manipulate it as needed from the webview preload script.
okay no problem, here is a possible work around. Maybe there is something you can look for in the page when you know the page may be finished loading and then trigger some code to manipulate the results. I'm thinking set up an interval to periodically do something and then remove the interval after some time or maybe even setup a timeout to run the manipulate code after a number of seconds have passed.
@TimNZ That's because you are waiting for the window load event before require jQuery. This means the document object is fully populated and the DOM is rendered.
You can't do what are you doing without the onload function
Can by any chance confirm, that the above example of manipulation www.google.com still works?
Got up to the point of preloading the script, in which i put an alert(); and works fine. But i cant get the text swapping work with the jquery.
Thanx
@dizawish I don't know what is going on now. I just tried the google manipulation code I wrote a long time ago and it fails in the preload script because window.onload never seems to be called or it's called before preload script loads now. This is different behavior to past Electron releases so I'm not really sure. Although I did manage to get it to work by refactoring my preload script a bit. I no longer rely on window.onload but instead use an interval to check the document.readystate and then inject jQuery, change the text in the google search input field and clear the interval. I've tested this against Electron 1.6.10 and 1.7.2 (beta).
@frankhale@dizawish Electron has changed, it appears window.onload may not of been fired ever, however updating the script to something like this will fix it:
Okay so disregard what I said above. It doesn't appear you can inject jQuery like that in a webview preload script. Try this, this works!
index.html:
<!DOCTYPE html>
<html>
<head>
<title>jQuery injection into webview preload</title>
</head>
<body style="overflow:hidden;">
<webview id="webview" preload="./manipulateGoogle.js" src="http://google.com" style="position:absolute;width:100%;height:100%;"></webview>
<script>
// need to have a script tag make webview work even if you don't plan to use it...
</script>
</body>
</html>
webview preload script (in this example name it manipulateGoogle.js)
// injection technique borrowed from http://stackoverflow.com/questions/840240/injecting-jquery-into-a-page-fails-when-using-google-ajax-libraries-apiwindow.onload=function(){varscript=document.createElement("script");script.src="https://code.jquery.com/jquery-2.1.4.min.js";script.onload=script.onreadystatechange=function(){$(document).ready(function(){$("#lst-ib").val("Hello, World!");});};document.body.appendChild(script);};
Activity
frankhale commentedon Jun 25, 2015
I'd probably do it this way. Instead of loading Google in the mainWindow using loadUrl I'd instead create a basic HTML page that contains a webview that loads Google. I'd then create a small JS file to load in the webview preload. This script would require jQuery like this:
Then in that small script I'd do whatever other code you need to do in order to intercept and manipulate elements on the Google page.
At least that is what I am thinking off the top of my head. I'm sure there are other ways to do it.
SantoshSrinivas79 commentedon Jun 25, 2015
That works. Thank you. :-)
SantoshSrinivas79 commentedon Jun 25, 2015
Actually, I am still stuck on getting the elements of the page. How do I access them?
frankhale commentedon Jun 25, 2015
I'll try to come up with an example. Give me a few minutes.
frankhale commentedon Jun 25, 2015
Okay so disregard what I said above. It doesn't appear you can inject jQuery like that in a webview preload script. Try this, this works!
index.html:
webview preload script (in this example name it manipulateGoogle.js)
main.js
Yay! It works! w00t!
SantoshSrinivas79 commentedon Jun 25, 2015
@frankhale Thanks a ton! My head was spinning understanding what is available in which process!
Thank you! :-)
frankhale commentedon Jun 25, 2015
You're welcome! Let me know if you have any other questions. I'll try to help out if I can!
Deliaz commentedon Feb 29, 2016
frankhale thanks, great info.
Is that possible in manipulateGoogle.js inject locally-stored scripts with relative path?
frankhale commentedon Feb 29, 2016
Webviews are capable of injecting local scripts by using the preload tag (see above example).
https://github.com/atom/electron/blob/master/docs/api/web-view-tag.md#preload
Deliaz commentedon Feb 29, 2016
Thx,
Is there any way to do this in the case of multiple scripts (without concatenation)?
frankhale commentedon Feb 29, 2016
I guess you could load a script that in turn loads other scripts to get around only being able to pass one script to preload tag.
Deliaz commentedon Feb 29, 2016
Yeah, I did so.
And it looks like I'm not able to use relative paths to scripts.
14 remaining items
frankhale commentedon Mar 30, 2016
So yeah, Electron has built in API to do IPC and the docs are here:
https://github.com/atom/electron/blob/master/docs/api/ipc-main.md
https://github.com/atom/electron/blob/master/docs/api/ipc-renderer.md
I've only briefly used the in-built IPC API so can't really comment much on it. I've also used Socket IO as a "IPC" mechanism and found it to work rather nicely. You could probably use other APIs to communicate back and forth but I haven't really explored it because Socket IO worked well for me in this regard.
3goats commentedon Mar 31, 2016
Thanks again @frankhale.
Going back to your
manipulateGoogle.jsexample which now after further investigation I understand whats happening. However, most of my page elements are created and built using AJAX requests which means there's nothing for the jquery selectors to find until the page is built.Appreciate, this is more of a jquery question (maybe), but is there a way to wait until the subsequent page build completes before firing
$(document).ready?frankhale commentedon Mar 31, 2016
(I haven't tried this but...) If you have control over the code that you are loading (eg you can edit it) why not set up a callback in your webview preload and then when your page gets done loading all it's ajax stuff then call the callback from the code loaded in the webview to notify it that your page is done doing everything so you can then manipulate it as needed from the webview preload script.
3goats commentedon Mar 31, 2016
Yeah, thats the problem, I don't have control of the code in the pages I'm loading.
frankhale commentedon Mar 31, 2016
okay no problem, here is a possible work around. Maybe there is something you can look for in the page when you know the page may be finished loading and then trigger some code to manipulate the results. I'm thinking set up an interval to periodically do something and then remove the interval after some time or maybe even setup a timeout to run the manipulate code after a number of seconds have passed.
TimNZ commentedon Aug 7, 2016
@frankhale You said you couldn't require jquery in a preload script
#2048 (comment)
Why was that? I'm doing this in 1.3.2 and it appears to work fine:
MarshallOfSound commentedon Aug 7, 2016
@TimNZ That's because you are waiting for the window load event before
requirejQuery. This means thedocumentobject is fully populated and the DOM is rendered.You can't do what are you doing without the
onloadfunctiondizawish commentedon May 27, 2017
Can by any chance confirm, that the above example of manipulation www.google.com still works?
Got up to the point of preloading the script, in which i put an alert(); and works fine. But i cant get the text swapping work with the jquery.
Thanx
frankhale commentedon May 29, 2017
@dizawish I don't know what is going on now. I just tried the google manipulation code I wrote a long time ago and it fails in the preload script because window.onload never seems to be called or it's called before preload script loads now. This is different behavior to past Electron releases so I'm not really sure. Although I did manage to get it to work by refactoring my preload script a bit. I no longer rely on window.onload but instead use an interval to check the document.readystate and then inject jQuery, change the text in the google search input field and clear the interval. I've tested this against Electron 1.6.10 and 1.7.2 (beta).
The following repository has this code:
https://github.com/frankhale/electron-inject-jquery-webview-example
michaelgv commentedon Sep 28, 2017
@frankhale @dizawish Electron has changed, it appears window.onload may not of been fired ever, however updating the script to something like this will fix it:
DOMContentLoaded is always fired, so listening for it is the best way to inject jQuery/do whatever you need to do here. I put it in a setTimeout for fun, however it's not necessary. Made a pull req on your repo @frankhale: https://github.com/frankhale/electron-inject-jquery-webview-example/pull/1
frankhale commentedon Sep 28, 2017
Thank you so much @michaelgv for the pull request. I've merged it.
michaelgv commentedon Sep 28, 2017
@frankhale np, another way to do it is with vanilla JS, or directly importing the jQuery library in your google.js file, vanilla way:
aryaveersr commentedon May 7, 2020
@frankhale A Easy way to load HTML file is : window.loadFile('index.html') than that __dir or whatever
fastcodeco commentedon Dec 1, 2020
Tks it worked
Novivy commentedon Dec 17, 2020
Note: You need to enable the webview tag for this to load