Getting functions from another script in JS

I am loading this JS code from the bookmarklet:

function in_array(a, b)
{
  for (i in b)
    if (b[i] == a)
      return true;
  return false;
}

function include_dom(script_filename) {
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}

var itemname = '';
var currency = '';
var price    = '';

var supported = new Array('www.amazon.com');
var domain = document.domain;

if (in_array(domain, supported))
{
  include_dom('http://localhost/bklts/parse/'+domain+'.js');
  alert(getName());
}
[...]

Please note that the function 'getName ()' is located at http: //localhost/bklts/parse/www.amazon.com/js . This code only works the second time that I click on the bookmarklet (the function does not seem to load until a warning () appears).

Oddly enough, if I changed the code to:

if (in_array(domain, supported))
{
  include_dom('http://localhost/bklts/parse/'+domain+'.js');
  alert('hello there');
  alert(getName());
}

I get both warnings on first click and the rest of the script functions. How can I make the script work with the first click of the bookmarklet without false warnings?

Thanks! -Mala

+3
source share
3 answers

<script> DHTML script , , , , script.

, , script. , , , , , , , , :

, <script> element, fnLoader script fnError , script.

, , (, ) ( ).

        tag.onload = fnLoader;
    tag.onerror = fnError;
    tag.onreadystatechange = function() {
        if (!window.opera && typeof tag.readyState == "string"){
            /* Disgusting IE fix */
            if (tag.readyState == "complete" || tag.readyState == "loaded") {
                fnLoader();
            } else if (tag.readyState != "loading") {
                fnError();
            };
        } else if (tag.readyState == 4) {
            if (tag.status != 200) {
                fnLoader();
            }
            else {
                fnError();
            };
        };
    });
+2

script (http://localhost/bklts/parse/www.amazon.com/js) . - , DOM, :

//...
if (in_array(domain, supported))
{
    include_dom('http://localhost/bklts/parse/'+domain+'.js');
    setTimeout(function() {
        alert(getName());
    }, 0);
}
//...

, -, . (, 10-100) , , , . , . , (?) , script.

0

: .

JS ( script , ), , src PHP , JS, document.domain . , php .

, -. , . - , , , :

:

javascript:(function(){document.body.appendChild(document.createElement('script')).src='http://localhost/bklts/div.php?d='+escape(document.domain);})();

/bklts/div.php:

<?php

print("
// JS code
");

$supported = array("www.amazon.com", "www.amazon.co.uk");
$domain = @$_GET['d']
if (in_array($domain, $supported))
    include("parse/$domain.js");

print("
// more JS code
");

?>
0

Source: https://habr.com/ru/post/1715234/


All Articles