How to open a blank tab and insert JS?

So i’m working on a side project which you can view here @ codepen --> https://codepen.io/Mike-was-here123/full/QOEGOj/

(not the best html, but it will do for now)

Here is a run down of how it works to better help answer the question that i will ask. First it obtained the input number of tab from the text box, and runs a for loop that, each interval, opens the current codepen, but on debug mode. This allows the program to close the tab, since you can only close opened pens in debug mode. This is the reason why codepen doesn’t close the current tab when you click on it since your in either editor or full screen mode. Since you did not open the tab you are prevented from closing it.


$(document).ready(function() {
  window.close()
});

Opening a codepen maybe 40 codepen tabs can be very slow when done in a couple seconds and cause errors/ crashes. This has led me to the conclusion to open a blank tab (window.open("")), then insert the tab “destruction” :arrow_up:️ code so it closes. How may i accomplish this? Would it be able to know i opened that tab and allow me to close it?


Just in case you skipped that whole entire explanation, my question is how to go about inserting JS code/ script into a blank tab i have opened using a program?

First off, please fix the link in your JavaScript so it is not just a hard coded link to your pen. I can’t view the debug mode of your pen, so I have to fork it, and then the link doesn’t work.

Okay ill disable the tab closing and opening code The loop where i open is at the bottom, just uncomment it from line 20 - 22

I’m not really sure why you want to do something like this, but you can use window.open() and document.write() in such a fashion:

var x = window.open('') // open a blank tab;
x.document.write('<script>alert("I was Injected")</script>');

Now this two methods have two problems:

1 - Injecting a script like this will be blocked by basically every server/domain to prevent cross domain attack…

And be thankful because otherwise I could insert in my website a script that looks into your open tabs, and if I find something interesting there I’d inject a nice script to take what I want from there :ghost:

2- I believe that document.write will cancel everything on a page if the script loads after said page.
But I’ll let you look at the documentation for additional info.

In conclusion what you want to achieve may be done if you were the server owner, and accept script incoming from a page of your own domain ( but still…)
On Codepen… I really don’t know

For some reason when i go to inject code i cant put a closing tag </script> or it just inserts my JS code from their on out into my current html.

This was the code i used:

var src= "jquery-3.2.1.min.js"
var x = window.open("").document.write('<script src = '+src+' type="text/javascript" >$(document).ready(function(){window.close()});')

Doesn’t work? If i check the inspect of the blank tab it is present.

So any idea on why the tab wont close?

I came accross and new function which you can view Here

 var x = window.open("")
     x.tabs.executeScript(x, {
       code: "window.close()"
     })

I think this is a start, still not working though. Any ideas?

That isn’t the Web API. That’s an API for writing Firefox extensions. You can write add-ons for browsers in JavaScript. Just to emphasise, that’s for the browser itself, not the web. They just happen to use the same language, that’s all. You can’t use functions for controlling a specific browser from within the js for a webpage

So how would i go about inserting code into a newly opened about:blank tab? I already tried

Just tried this on Chrome and worked, perhaps you are making some syntax mistake?

This was the issue i’m getting with that. It wont even let my run my loop either.

I was able to make that code run from the Browser’s console, however looks to me that CodePen is sanitising the code you write to prevent malicious code to be run.

As I told you last time, trying to do this kind of operation is always risky since it’s borderline with script injection.

You may have an easier time If you try to run the code on your own domain :slight_smile:

EDIT: I suspect is CodePen conflicting with the script tag because this works as expected.

var x = window.open('');
x.document.write('<div>test</div>');

Perhaps there’s something in their documentation that can help you further

For some reason it goes in, yet it just wont run.

EDIT: I suspect is CodePen conflicting with the script tag because this works as expected.

That is exactly was i suspected too. I was able to do what i am talking about with my own codepen. I would open it in debug mode, and since my codepen contains a self.close() command it will close once it is opened. This caused lag after a couple hundred tabs compared to a blank tab.

More on…Open new window in JS