0

What i'd like to do is scrape some data from a page and then open another page in a new tab, and insert the scraped data in a form contained in the new page.

The closest i got to open the new tab is:

function openNewBackgroundTab(){
var a = document.createElement("a");
a.href = "http://www.example.com";
var evt = document.createEvent("MouseEvents");
//the tenth parameter of initMouseEvent sets ctrl key
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0,
                            true, false, false, false, 0, null);
a.dispatchEvent(evt);
}    

But, as the function name says the tab is opened in the background and it is not focused.

Is there a way to do it in chrome using a js bookmarklet?

pietrovismara
  • 6,102
  • 5
  • 33
  • 45
  • Possible duplicate of: http://stackoverflow.com/questions/2704206/how-to-change-browser-focus-from-one-tab-to-another Anyway, due to security reasons you can't rly force a browser to set focus on a newly opened tab. – icecub Oct 14 '14 at 21:18

1 Answers1

0

I had a similar task where I had to print a form in a Silverlight app. Due to the fact that printing is Silverlight sucks I used some JavaScript to open a new window and write a form to it.

It's not a tab, but a window. Maybe it helps:

var print = function (data) {
    var html = 'This is a test';
    var newWindow = window.open('');
    newWindow.document.write('<html><head><title>New TAB</tit' + 'le>');
    newWindow.document.write('</he' + 'ad><body>');
    newWindow.document.write(html);
    newWindow.document.write('</bo' + 'dy></ht' + 'ml>');
    newWindow.document.close();
    newWindow.focus();
};

print();

Also, if you continue with your approach, IE has some particularities for triggering a click event on an anchor. You might want to check this or something similar.

Community
  • 1
  • 1
Razvan
  • 3,017
  • 1
  • 26
  • 35
  • The problem is that i need to use a form in an existent page, not to print a new one... – pietrovismara Oct 15 '14 at 11:11
  • You can render the form in the new opened window. In my example the window contains just the string `'This is a test'`, but you can replace it with any html. – Razvan Oct 15 '14 at 11:28