簡體   English   中英

iMacros和socket.io

[英]iMacros and socket.io

在我的iMacros腳本中,我嘗試使用socket.io連接到服務器。 首先,我必須獲取socket.io.js文件,對其進行評估,然后像往常一樣使用socket.io。 這是我的腳本的摘錄:

    function loadScriptFromURL(url) {
        try {
            var request = Components.classes['@mozilla.org/xmlextras/xmlhttprequest;1'].createInstance(Components.interfaces.nsIXMLHttpRequest);
            request.open('GET', url, false);
            request.send();
            if (request.status !== 200) {
                var message = 'There was an error trying to connect to: ' + url + ', request status: ' + request.status;
                iimDisplay(message);
            }
            eval(request.response);
        } catch (e) {
            window.console.log(e);
            return false;
        }
        return true;
        //alert(request.respsonse);
    }

    loadScriptFromURL('http://localhost:3700/socket.io/socket.io.js');
    window.console.log(io.version); // returns 0.9.16
    var socket = io.connect('http://localhost:3700');

問題是由iMacros執行的Javascript代碼在插件的范圍內(沙箱),當我使用io的connect方法時,出現“ io.util is undefined”錯誤。 有沒有一種方法可以填充此方法,以便iMacros可以看到它? 我想我需要在socket.io.js文件中進行一些更改,但還不太確定...

我花了幾天時間找到解決方案。 我還有另一個錯誤,例如“ setTimeout()不是一個函數,Blob不是一個函數,有許多未定義的錯誤...”,這是由於代碼未在當前窗口的上下文中運行而發生的。

解:

轉到“ /node_modules/socket.io-client\\socket.io.js”

這是來自網址“ http:// localhost:3000 / socket.io / socket.io.js ”的客戶端javascript代碼

全部替換:

"new Blob" -> "new window.Blob"
"instanceof Blob" -> "instanceof window.Blob"

And to do the same for:
Filereader - > window.Filereader
setTimeout() - > window.setTimeout()
clearTimeout() - > window.clearTimeout()
setTimeout() - > window.setTimeout()

它為我工作。 Node.JS版本為5.3.0,Socket.IO版本為1.4.5。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM