appendChild doesn't work on new window using window.open() - javascript

I've got a problem with script that works well on when I run it on current tab but when I try to use it on other widow using window.open it doesn't work. The simplified version of code looks like this:
var win = window.open("some_url");
var new_element = win.document.createElement('div');
new_element.textContent = "some text";
win.document.head.appendChild(new_element);
Im using chrome snippet tester in dev tools and it works perfectly fine when i run this stript on current window. I know it doesn't work in new widow because inspecting doesn't show any new elements. Does anybody know why it won't work? I have zero knowledge about the order of DOM creation and so on. Maybe I can't append to it until its already loaded?

After some digging I've finally got not a solution but an anwser. The reason why code did work on blank page but not on some real url's is because normally websites use Cross-site scripting blocks that are blocking such actions. So there was nothing wrong with the code. It was the site that was blocking it's execution.

Related

Is there a way to execute rest of the script after performing window.location change?

Working on small chrome extension using manifest v3. Basically I have a line in my code that changes the window location to another page (it is required). The following line is supposed to find the element once the window has changed.
abstract:
window.location = "link to other web"
const element = document.querySelector("div")
console.log(element)
The problem is of course the fact that lines after window.location doesn't work because they do not exist anymore. Is there a way to keep the logic after the refresh/reload?
Perfectly if it would work like selenium (in brower, not in window). As mentioned I'm using manifest v3 api so maybe there is a way to keep the code...

Bookmarklet works fine in Opera, but when running in Firefox I get extremely weird results

I wrote this bookmarklet:
javascript:var b = document.createElement("button");b.innerHTML = "Scroll to current video";b.addEventListener("click",() => doItYesly());b.style.position = "fixed";b.style.left = 0;b.style.top = 0;b.style.zIndex = "99999999";document.body.prepend(b);var s = document.createElement("button");s.style.position = "fixed";s.style.left = 0;s.style.top = "50px";s.style.zIndex = "99999999";s.innerHTML = "Set";s.addEventListener("click",() => localStorage.setItem("scrolldistanceforosautoscroller",window.scrollY));document.body.prepend(s);function doItYesly(){let inter = setInterval(() => {scrollTo(0,parseInt(localStorage.getItem("scrolldistanceforosautoscroller")));if(window.scrollY === parseInt(localStorage.getItem("scrolldistanceforosautoscroller"))){clearInterval(inter);}},100);}window
Basically it's a tiny bookmarklet to allow a user to auto-scroll to a specific point in a long list of videos on YouTube. I wrote it for a friend of mine who repeatedly navigates to the same page over and over, and wanted to try and save some time and prevent from having to manually scroll through all the videos every time he wanted to return to the exact same spot.
When I click this bookmark in my browser (I use Opera) it works just fine. When I open the same bookmark in Firefox (he uses Firefox) it redirects me to a page that says "this page is hosted on your computer" and it simply says [object Window]. This is because, in Opera when I would run my code it would print out "input scroll distance", because the last expression evaluated to that. To fix that, I simply pointed to the window object, causing the final expression to evaluate to the page itself. This fixed the problem for Opera, but for Firefox it doesn't fix the issue... Instead of just rendering the page like usual, it simply outputs a textual representation of the window object...
Is there any way around this? I assume this is for security, and if so then there probably isn't a workaround... But perhaps there's something I could do to make Firefox stop behaving this way?
When I run this same exact code from the dev console it works perfectly as expected, the problem only occurs when I save it as a bookmarklet and click on it. Any ideas?
Sadly bookmarklets were removed from Opera due to "security reasons". In other browsers like Firefox or Chrome, you can use this bookmarklet template to separate the bookmarklet code from the existing JavaScript code:
javascript:(function(){CONTENTGOESHERE})();
Replace CONTENTGOESHERE with your code.

Edit opened HTML Page, with Javascript

I was looking into making Firefox addons, and I need some help.
Is it possible to edit an HTML page that is open in the browser with javascript?
For example:
User types in "google.com"
Addon is activated
Javascript changes contents of "google.com" to maybe say "Hello!" at the bottom.
Of course this isn't specifically what I want to do, but a push in the right direction on how to accomplish such a task would be great.
~Carpetfizz
From within a Firefox addon this is obviously possible as many extensions do this.
If you, however, simply want to modify the DOM and nothing else than I would recommend taking a look at greasemonkey. Loads of example scripts around to do this: http://userscripts.org/
And the added benefit, if written correctly they also work in Chrome and other browsers.
Yes, it is. You must find a tutorial about javascript DOM manipulation

JavaScript document write in IE8

This code below runs with jaavascript error in IE 8 browser after window.open('','','width=200,height=100') line of code gets executed. New window gets open but it runs with error "jQuery is undefined". Here I do not use jQuery at all, but sure, I use it all across the site.
var newWindow = window.open('','','width=200,height=100')
newWindow.document.write(someHmtlAsString);
newWindow.document.close();
newWindow.focus();
Does anyone have suggestion why is this so, or is there some bug in IE (hack for IE) which would eliminate javascript error while page renders?
Thanks
It looks like someHmtlAsString that you insert contains some <script> tag that tries to use jQuery. Inspect its content and if that's the case, add tag to load jQuery to it or change code not to use it.
You should set the source of new windows and iframes to about:blank if you want control over them.
You also want to use newWindow.contentDocument || newWindow.contentWindow.document
And it might be a good idea to open() the document before you write() to it.
Update: forgot this:
If you open the window about:blank, it needs time to load..
So you cannot write to it at once!!
So either check if it is loaded (onload), then have it write the source (I prefer this).
OR set timeout of about 50ms (usually) and then write to the new window/iframe.
also note that xhtml does not support document.write!!
Good luck!

IE and Selenium: window.createPopup()

I've been playing with Selenium lately, trying to create tests for an IE only application. Things were progressing (though slowly as without the recorder plugin I had to resort to trial and error to try to find the appropriate element paths), but now I'm stuck with a problem related to popup menues.
Most of the application actions are triggered from a popup menu created with javascript window.createPopup() and I can't seem to find a way to send events to elements inside the popup.
Maybe I should be selecting the popup like I do for windows opened with window.open(...), which are working fine BTW. I tried assigning a name to the popup menu returned by createPopup() and treating it the same way I treat windows but that doesn't seem to be working.
Does anybody knows if this is supposed to work? Any help will be appreciated.
Thanks,
Unfortunately, no. window.createPopup isn't accessible to Selenium. Being an IE only feature it has really limited portability and generally isn't a best practice. I know that's of little consolation to you, because I assume your stuck with someone else's code that's used createPopup.
The real problem is that craeatePopup doesn't add anything to the DOM. Try opening a popup object and viewing it's source. You'll see this:
<html><body></body></html>
So there's nothing really there for Selenium to grab hold of.
What does the popup do for your application? You indicated it provides some navigation, can you just navigate to those pages directly?
if you know the name of the window you can do
selenium.click("elementToLaunchPopup");
selenium.waitForPopup("nameOfWindow",30000);
selenium.selectWindow("nameOfWindow");
// rest of your test
To get back to the main window you will need to selenium.selectWindow("null");
I am using selenium 2.0b3 with InternetExplorerDriver. I found something that do the trick.
In your js save a reference to the popup window.
var popUp= window.createPopup();
Then in your java code:
public Object executeJS(String code){
JavascriptExecutor js = (JavascriptExecutor) driver;
return js.executeScript(code);
}
WebElement popUp =(WebElement)
executeJS("return popUp.document.documentElement;");
This will give you a reference to the page and you can find elements normally.

Categories

Resources