Solved: About blank instead of content


(Egon) #1

i’m running the code.js from background.js.

background.js contains

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(tab.id, {file: "code.js"})
});

code.js contains

(function(d){var f=d.createElement('form');f.action='http://example.com/run.html?bm';f.method='post';var i=d.createElement('input');i.type='hidden';i.name='url';i.value=document.location.href;f.appendChild(i);d.body.appendChild(f);f.submit();})(window.open().document)

But instead of transferring of current URL with POST to http://example.com/run.html?bm and load related page, as i get it in Chrome - i get only an empty tab (about:blank).

How can i run this POST in FF?


(Martin Giger) #2

I can imagine a couple of reasons without actually testing this myself. One would be that your code can’t access the document of window.open(), since it’s about:blank. Another one is that you’re running too early to have a document (which I have to admit, I do not know window.open well enough).

My solution suggestion would be to use the tabs API instead to read the current URL and open a tab and set the URL to a data: document or an extension html page that then does the POST redirect to your thing.


(Egon) #3

Are your imaginations relevant knowing that this code is working in Chrome?


(Martin Giger) #4

Yes, because an empty window doesn’t have any reason to behave exactly the same across browsers.

Either way, it should be deductible by debugging what’s actually going on - if anything at all…


(Tian, Sheng) #5

trying to modify the document of about:blank will cause a series of problems. I would suggest you change it to this way:

  • Write an static html page which parse its parameter set in its form and post
  • Add it to web accessible resource list
  • Open this page in your browserAction onClick callback with the url parameter of the current tab

(Egon) #6

I try to do it now on the way mentioned in https://stackoverflow.com/a/37908997/1992004:

background.js:

chrome.browserAction.onClicked.addListener(tab => {
    chrome.tabs.create({ url: 'http://gtmetrix.com/analyze.html?bm', function (tab) {
    browser.tabs.executeScript(tab.id, { file: "submitForm.js" },});
	});

submitForm.js

function submitForm(request, sender, sendResponse)
{
    var f = document.createElement('form');
    f.setAttribute('method','post');
    f.setAttribute('action','http://gtmetrix.com/analyze.html?bm');

    var i = document.createElement('input');
    i.setAttribute('type','hidden');
    i.setAttribute('name','url]');
    i.setAttribute('value', request.url);
    f.appendChild(i);

    document.getElementsByTagName('body')[0].appendChild(f);
    f.submit();
}

chrome.runtime.onMessage.addListener(submitForm);

But i currently absolutely miss debugging skills for FF :frowning: - doesn’t work, don’t know why:)


(Egon) #7

I’m going now another way: I open firstly an empty tab with a script execution and sending a message, like this:

chrome.browserAction.onClicked.addListener(tabToAnalyze => {
    chrome.tabs.create({url: 'about:blank'}, blankTab => {
        browser.tabs.executeScript(blankTab.id, {file: 'submitForm.js', matchAboutBlank: true}, () => {
            chrome.tabs.sendMessage(blankTab.id, {url: tabToAnalyze.url});
        });
    });
})

Than, in the submitForm.js i execute the code with the message listener, like this:

function submitForm(request, sender, sendResponse) {

    var f = document.createElement('form');
    f.setAttribute('method','post');
    f.setAttribute('action','https://gtmetrix.com/analyze.html?bm');

    var i = document.createElement('input');
    i.setAttribute('type','hidden');
    i.setAttribute('name','url');
    i.setAttribute('value', request.url);
    f.appendChild(i);

    document.getElementsByTagName('body')[0].appendChild(f);
    f.submit();
}

browser.runtime.onMessage.addListener(submitForm);

New tab is indeed opening, but… remains empty grrrr :frowning:


(Egon) #8

With help of swarm knowledge got it to work:

the code from my previous answer is working. But! It works ONLY after FF57 - and me, the great thinker, was testing it with FF 49. The cause of this fail is in usage of matchAboutBlank: true - which is properly working in newer FF versions.

Nevertheless - thanks to all!

And now - a bit of advertising :slight_smile: Wanna see, what i was coding? https://addons.mozilla.org/de/firefox/addon/all-in-one-pagespeed-test/