如何获取一个脚本的结果并将其提供给另一个脚本?

时间:2017-02-08 00:12:44

标签: javascript

我在这里过头,需要一些帮助才能理解我正在看的东西! (Javascript非常新!)这是我理解的情况......

我有一个脚本从文本段落中选择一行,并且当前生成此警报,其中“1”是所选行:

alert(getLine("sourcePara", 1));

...我不需要触发警报,而是需要将所选文本输入到这个单独的脚本中,该脚本将数据发送到另一个浏览器窗口。目前它正在从ID为“STOCK1”的表单中获取文本字段,但可以替换它:

function sendLog() {
    var msg = document.getElementById('STOCK1').value;
    t.send('STK1', msg);
}

我完全不知道这个文本数据在第一个脚本的出路上采用了什么形式,并且不知道如何将其作为第二个脚本的来源调用...帮助!

非常感谢!

编辑: 以下是Local Connection元素的源代码;

function LocalConnection(options) {

    this.name = 'localconnection';

    this.id = new Date().getTime();

    this.useLocalStorage = false;

    this.debug = false;

    this._actions= [];

    this.init = function(options) {

        try {
            localStorage.setItem(this.id, this.id);
            localStorage.removeItem(this.id);
            this.useLocalStorage = true;
        } catch(e) {
            this.useLocalStorage = false;
        }
        for (var o in options) {
            this[o] = options[o];
        }
        this.clear();
    }

    this.listen = function() {
        if (this.useLocalStorage) {
            if (window.addEventListener) {
                window.addEventListener('storage', this.bind(this, this._check), false);
            } else {
                window.attachEvent('onstorage', this.bind(this, this._check));
            }
        } else {
            setInterval(this.bind(this, this._check), 100);
        }
    }

    this.send = function(event) {
        var args = Array.prototype.slice.call(arguments, 1);
        return this._write(event, args);
    }

    this.addCallback = function(event, func, scope) {
        if (scope == undefined) {
            scope = this;
        }
        if (this._actions[event] == undefined) {
            this._actions[event] = [];
        }
        this._actions[event].push({f: func, s: scope});
    }

    this.removeCallback = function(event) {
        for (var e in this._actions) {
            if (e == event) {
                delete this._actions[e];
                break;
            }
        }
    }

    this._check = function() {
        var data = this._read();
        if (data.length > 0) {
            for (var e in data) {
                this._receive(data[e].event, data[e].args);
            }
        }
    }

    this._receive = function(event, args) {
        if (this._actions[event] != undefined) {
            for (var func in this._actions[event]) {
                if (this._actions[event].hasOwnProperty(func)) {
                    this.log('Triggering callback "'+event+'"', this._actions[event]);
                    var callback = this._actions[event][func];
                    callback.f.apply(callback.s, args);
                }
            }
        }
    };

    this._write = function(event, args) {
        var events = this._getEvents();
        var evt = {
            id: this.id,
            event: event,
            args: args
        };
        events.push(evt);
        this.log('Sending event', evt);
        if (this.useLocalStorage) {
            localStorage.setItem(this.name, JSON.stringify(events));
        } else {
            document.cookie = this.name + '=' + JSON.stringify(events) + "; path=/";
        }
        return true;
    }

    this._read = function() {
        var events = this._getEvents();
        if (events == '') {
            return false;
        }
        var ret = [];

        for (var e in events) {
            if (events[e].id != this.id) {
                ret.push({
                    event: events[e].event,
                    args: events[e].args
                });
                events.splice(e, 1);
            }
        }
        if (this.useLocalStorage) {
            localStorage.setItem(this.name, JSON.stringify(events));
        } else {
            document.cookie = this.name + '=' + JSON.stringify(events) + "; path=/";
        }
        return ret;
    }

    this._getEvents = function() {
        return this.useLocalStorage ? this._getLocalStorage() : this._getCookie();
    }

    this._getLocalStorage = function() {
        var events = localStorage.getItem(this.name);
        if (events == null) {
            return [];
        }
        return JSON.parse(events);
    }

    this._getCookie = function() {
        var ca = document.cookie.split(';');
        var data;
        for (var i=0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') {
                c = c.substring(1, c.length);
            }
            if (c.indexOf(this.name+'=') == 0) {
                data = c.substring(this.name.length+1, c.length);
                break;
            }
        }
        data = data || '[]';
        return JSON.parse(data);
    }

    this.clear = function() {
        if (this.useLocalStorage) {
            localStorage.removeItem(this.name);
        } else {
            document.cookie = this.name + "=; path=/";
        }
    }

    this.bind = function(scope, fn) {
        return function () {
            fn.apply(scope, arguments);
        };
    }

    this.log = function() {
        if (!this.debug) {
            return;
        }
        if (console) {
            console.log(Array.prototype.slice.call(arguments));
        }
    }
    this.init(options);
}

1 个答案:

答案 0 :(得分:0)

如果我理解你的要求是正确的,那么我认为将日志功能更改为以下内容:

function sendLog() {
    t.send('STK1', getLine("sourcePara", 1));
}

这假设getLine可以全局访问。

另外另一种方法是允许sendLog函数将消息作为参数。在这种情况下,您可以将第一个脚本更改为:

sendLog(getLine("sourcePara", 1));

修改后的sendLog函数如下所示:

function sendLog(msg) {
    t.send('STK1', msg);
}

LocalConnection.js应该处理在窗口/标签之间传输数据。看起来像一个迭代的项目:

https://github.com/jeremyharris/LocalConnection.js