javascript - HTML 头部中的两个脚本冲突 (Brython & iFlyChat)

标签 javascript brython

我正在加载 Brython 和 iFlyChat,但如果未注释 iFlyChat 脚本,Brython 将无法工作。我尝试了各种异步组合,但似乎有一些更基本的东西。

JSFiddle here和下面的代码:

https://jsfiddle.net/tutmoses/c09dhbrq/

<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Title</title>

<!-- BRYTHON -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.8/brython.js" integrity="sha256-rA89wPrTJJQFWJaZveKW8jpdmC3t5F9rRkPyBjz8G04=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.8/brython_stdlib.js" integrity="sha256-Gnrw9tIjrsXcZSCh/wos5Jrpn0bNVNFJuNJI9d71TDs=" crossorigin="anonymous"></script>

<!-- IFLYCHAT 
<script>
console.log("init iflychat plugin");
var iflychat_app_id="1c0d1abc-4864-4608-acb7-6cdbfab07ce2";
var iflychat_external_cdn_host="cdn.iflychat.com",iflychat_bundle=document.createElement("SCRIPT");iflychat_bundle.src="//"+iflychat_external_cdn_host+"/js/iflychat-v2.min.js?app_id="+iflychat_app_id,iflychat_bundle.async="async",document.body.appendChild(iflychat_bundle);var iflychat_popup=document.createElement("DIV");iflychat_popup.className="iflychat-popup",document.body.appendChild(iflychat_popup);
</script> -->

</head>
<body onload="brython()">

  <div class="container">

    <h2 id="hello"></h2>
    <button id="alert-btn">Alert & Insert</button>

    <input type="text" id="text" placeholder="Enter something">
    <span id="output"></span>

</div>
<!-- Alert & DOM insert -->
<script type="text/python" id="script0">
    from browser import document, console, alert

    def show(e):
        console.log(e)
        alert('Hello World')
        document['hello'] <= 'Hello World'

    document['alert-btn'].bind('click', show)
</script>

<!-- Text bind -->
<script type="text/python" id="script1">
    from browser import document

    def show_text(e):
        document['output'].textContent = e.target.value;

    document['text'].bind('input', show_text)
</script>

</body>
</html>

最佳答案

你正在做的是你在这里使用两种语言来运行脚本,
1. 使用Javascript初始化iFlyChat 2.输入框的Python语言

解释启用两个脚本时的场景:
onload用于初始化 brython 的方法永远不会被调用,因为两个脚本之间存在竞争条件。推迟一段时间可以解决我们的问题。

解决了:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Title</title>

    <!-- BRYTHON -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.8/brython.js" integrity="sha256-rA89wPrTJJQFWJaZveKW8jpdmC3t5F9rRkPyBjz8G04=" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.8/brython_stdlib.js" integrity="sha256-Gnrw9tIjrsXcZSCh/wos5Jrpn0bNVNFJuNJI9d71TDs=" crossorigin="anonymous"></script>

</head>
<body>

<div class="container">

    <h2 id="hello"></h2>
    <button id="alert-btn">Alert & Insert</button>
    <input type="text" id="text" placeholder="Enter something">
    <span id="output"></span>

</div>
<!--Calling it first because it has javascript language and specially mentioned "text/javascript" in type here -->
<!-- IFLYCHAT  -->
<script type="text/javascript">
    console.log("init iflychat plugin");
    var iflychat_app_id="1c0d1abc-4864-4608-acb7-6cdbfab07ce2";
    var iflychat_external_cdn_host="cdn.iflychat.com",iflychat_bundle=document.createElement("SCRIPT");iflychat_bundle.src="//"+iflychat_external_cdn_host+"/js/iflychat-v2.min.js?app_id="+iflychat_app_id,document.body.appendChild(iflychat_bundle);var iflychat_popup=document.createElement("DIV");iflychat_popup.className="iflychat-popup",document.body.appendChild(iflychat_popup);
</script>

<!-- now initializing brython with a delay of 10ms-->
<script type="text/javascript">
    setTimeout(() => {
        console.log("init Brython");
        window.brython();
    }, 10);
</script>

<!-- Alert & DOM insert -->
<script type="text/python" id="script0">
    from browser import document, console, alert

    def show(e):
        console.log(e)
        alert('Hello World')
        document['hello'] <= 'Hello World'

    document['alert-btn'].bind('click', show)
</script>

<!-- Text bind -->
<script type="text/python" id="script1">
    from browser import document

    def show_text(e):
        document['output'].textContent = e.target.value;

    document['text'].bind('input', show_text)
</script>

</body>
</html>


https://jsfiddle.net/td0xpqLz/2/

关于javascript - HTML 头部中的两个脚本冲突 (Brython & iFlyChat),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62083257/

相关文章:

javascript - 填写现有字段后如何在表单中添加一组输入字段

python - 如何使用 brython 导入 numpy 或其他库

javascript - Nashorn:BigDecimal 与数字比较的严格相等

javascript - 使用 Javascript 检测两个字符串之间的差异

Javascript-如何检查此事件的操作是否已完成

javascript - HTML、Python、Brython、javascript - 在 Brython 上运行

python - 如何在页面上查看 Brython 脚本的输出?为什么 `print` 不起作用?

javascript - 如何使用相同的表格使用 quickForm 更新/插入记录?

python - 客户端和服务器端使用相同的渲染代码

javascript - python markdown哪个版本有mark方法