haterapps / fake-data

Fake Data - A form filler you won't hate
39 stars 2 forks source link

hostname seem to get fake-data extension-name instead of site url #13

Closed PerHermanssonPrivat closed 2 years ago

PerHermanssonPrivat commented 2 years ago

Hi

Just starting to test out fakedata filler, seems exellent for my needs. But ...

I use on several test/staging-sites with same forms, and want to get part of the site url into the form when filling form. But it seems the window.location.hostname gets the url of the extension, not the site. (First I thought it was just when building the JS/generator, but happens on actual site when filling form too.)

Suggestions what I can do to work around this ?


prefix_text = "FieldStart ";

var orig_winhostname = window.location.hostname;
var partial_winhostname = orig_winhostname.slice(0,6);

var min = 10000;
var max = 99990;
var rand = Math.floor(Math.random() * (max - min + 1)) + min;

return prefix_text + partial_winhostname + " Something" + rand;

Gives me "FieldStart kaabap Something23876"

But I am on site "https://stage3.example.com/" and wanted the "stage3" part.

haterapps commented 2 years ago

Hello,

The reason you are getting this result is because Fake Data executes the codes in an internal background page, which is isolated from the main web page that you are viewing.

There is a workaround though. If you return a function from your code, that function will be executed in the context of your web page and will fill the result from it.

So for your particular example, in order to make it work you could try something like this:

return function() {
    prefix_text = "FieldStart ";

    var orig_winhostname = window.location.hostname;
    var partial_winhostname = orig_winhostname.slice(0,6);

    var min = 10000;
    var max = 99990;
    var rand = Math.floor(Math.random() * (max - min + 1)) + min;

    return prefix_text + partial_winhostname + " Something" + rand;

}

Let me know if that works.

PerHermanssonPrivat commented 2 years ago

That worked like a charm, much appreciated!