window.open is not opening the popup

2

I have an extension that sends things to an external server by ajax post (all in javascript) and the response from the send (sent from the server) is received in the extension itself. I want to put that answer in another popup. How can I do this? I tried with window.open (..., '_ self', ...) but did not, did not open the window. Anyone help out there? obs: I do not know how to use jquery

    
asked by anonymous 28.03.2015 / 18:11

1 answer

1

I've done an example extension that lets you do what you asked for.

In the manifest.json file, you must add a permission to access the host, otherwise you will not be able to run ajax. In case, I'll use an echo page from jsfiddle:

"permissions": [
    "http://jsfiddle.net/"   // aqui está a permissão necessária, para acessar o host
]

The rest is just like normal web development ... you can even use jQuery. I put the jquery file together with the extension archives. The version used is the one that is linked in the file listing below.

Files

  • jquery-1.11.2.min.js

  • manifest.json

    {
      "manifest_version": 2,
    
      "name": "Extensão de teste, com Ajax e Janela de Pop-up",
      "description": "Extensão de teste, com Ajax e Janela de Pop-up",
      "version": "1.0",
    
      "browser_action": {
        "default_popup": "popup.html"
      },
    
      "permissions": [
        "http://jsfiddle.net/"
      ]
    }
    
  • popup.html

    <!doctype html>
    <html>
      <head>
        <title>Abrindo resultado de ajax em outra tela</title>
        <script src="jquery-1.11.2.min.js"></script>
        <script src="popup.js"></script>
      </head>
      <body>
        <button id="btn">Abrir conteúdo em outra janela</button>
      </body>
    </html>
    
  • popup.js

    $(function() {
        $('#btn').click(function() {
                $.ajax({
                    url: 'http://jsfiddle.net/echo/html/',
                    data: {
                        html: '<div>Documento carregado via ajax!</div>',
                        delay: 1
                    },
                    type: 'POST',
                    success: function(data){
                        var win = window.open(
                            "",
                            "Title",
                            "toolbar=no,"+
                            "location=no,"+
                            "directories=no,"+
                            "status=no,"+
                            "menubar=no,"+
                            "scrollbars=yes,"+
                            "resizable=yes,"+
                            "width=780,"+
                            "height=200,"+
                            "top="+(screen.height-400)+","+
                            "left="+(screen.width-840));
                        win.document.body.innerHTML = data;
                    },
                    error: function() {
                        alert("error: "+JSON.stringify(arguments));
                    }
                });
                return false;
            });
    });
    
29.03.2015 / 01:54