gpt4 book ai didi

javascript - 如何获取一个脚本的结果并将其输入到另一个脚本中?

转载 作者:行者123 更新时间:2023-11-28 05:01:34 27 4
gpt4 key购买 nike

我有点不知所措,需要一些帮助才能理解我在看什么! (对 Javascript 来说非常陌生!)这是我所理解的情况......

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

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

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

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

我完全困惑于这个文本数据在第一个脚本中采取的形式,并且不知道如何将其作为第二个脚本的源调用......帮助!

非常感谢!

编辑:这是本地连接元素的源代码;

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);
}

最佳答案

如果我正确理解您的要求,那么我认为将日志函数更改为以下内容即可:

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

关于javascript - 如何获取一个脚本的结果并将其输入到另一个脚本中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42102400/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com