123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119(* Js_of_ocaml library
* http://www.ocsigen.org/js_of_ocaml/
* Copyright (C) 2014 Hugo Heuzard
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)openJs_of_ocamlopen!Importletrecrandom_identifiersize=letb=Buffer.createsizeinfor_i=0tosize-1doBuffer.add_charb(Char.chr(97+Random.int26))done;lets=Buffer.contentsbinifJs.Optdef.test(Js.Unsafe.getDom_html.window(Js.strings))then(* name already in use, choose another one*)random_identifiersizeelsesletraw_callnameurierror_cbuser_cb=letscript=Dom_html.(createScriptdocument)inletfinalize()=Js.Unsafe.deleteDom_html.window(Js.stringname);Js.Opt.iterscript##.parentNode(funparent->Dom.removeChildparentscript)inletexecuted=reffalseinJs.Unsafe.setDom_html.window(Js.stringname)(funx->executed:=true;finalize();user_cbx);script##.src:=Js.stringuri;script##._type:=Js.string"text/javascript";script##.async:=Js._true;((Js.Unsafe.coercescript)##.onerror:=funx->finalize();error_cbx);((Js.Unsafe.coercescript)##.onload:=funx->Lwt.async(fun()->Lwt.bind(Lwt_js.sleep1.)(fun()->if!executedthenLwt.return_unitelse(Firebug.console##warn(Js.string"Jsonp: script loaded but callback not executed");finalize();error_cbx;Lwt.return_unit))));letinit()=ignore(Dom.appendChildDom_html.document##.bodyscript)ininit,finalizeletcall_prefixmake_urierror_cbuser_cb=letname=prefix^random_identifier10inleturi=make_urinameinraw_callnameurierror_cbuser_cbletcall_custom_url?timeout?(prefix="")make_uri=lett,w=Lwt.task()inletinit,finalize=call_prefixmake_uri(fun_->Lwt.cancelt)(Lwt.wakeupw)inLwt.on_canceltfinalize;letnew_t=matchtimeoutwith|None->t|Somedelay->letwait=Lwt.bind(Lwt_js.sleepdelay)(fun()->Lwt.cancelt;t)inLwt.choose[wait;t]ininit();new_tletadd_paramnamevaluel=letl=List.filter(fun(x,_)->not(String.equalxname))lin(name,value)::lletcall?timeout?(param="callback")?(prefix="")url=letmake_uricbname=matchUrl.url_of_stringurlwith|None->failwith"Jsonp.call: Cannot parse url"|Someurl->letnew_url=matchurlwith|Url.Httphttp->Url.Http{httpwithUrl.hu_arguments=add_paramparamcbnamehttp.Url.hu_arguments}|Url.Httpshttp->Url.Https{httpwithUrl.hu_arguments=add_paramparamcbnamehttp.Url.hu_arguments}|Url.Filefile->Url.File{filewithUrl.fu_arguments=add_paramparamcbnamefile.Url.fu_arguments}inUrl.string_of_urlnew_urlincall_custom_url?timeout~prefixmake_uri