gpt4 book ai didi

javascript - 如何将参数传递给服务器端书签?

转载 作者:行者123 更新时间:2023-11-28 16:26:17 25 4
gpt4 key购买 nike

我有这个小书签,它遵循 this topic 中的建议。 :

javascript: (function () {  
var jsCode = document.createElement('script');
jsCode.setAttribute('src', 'http://path/to/external/file.js');
document.body.appendChild(jsCode);
}());

我的问题是,如何为服务器脚本提供一些参数(例如页面标题)?服务器脚本如下(到目前为止只是一些 PoC):

(function(message){
alert(message + " " + Math.random()*100);
})();

最佳答案

要将参数传递给服务器端脚本,请扩展查询字符串:

    jsCode.setAttribute('src', 'http://path/to/external/file.js?parameter=' + document.title);

如果您打算将参数传递给返回的脚本,请将 onload 处理程序添加到脚本元素:

javascript:(function() {
var jsCode = document.createElement('script');
var parameter = document.title; //Define parameter
jsCode.onload = function() {
// If the function exists, use:
if(typeof special_func == "function") special_func(parameter);
};
jsCode.setAttribute('src', 'http://path/to/external/file.js');
document.body.appendChild(jsCode);
})();

响应脚本(file.js):

function special_func(param){
alert(param);
}

更新/示例

如果您想将(多个)变量传递给响应脚本,请使用以下代码:

javascript:(function() {
var jsCode = document.createElement('script');
var params = ["test", document.title]; //Define parameter(s)
var thisObject = {}; // `this` inside the callback function will point to
// the object as defined here.
jsCode.onload = function() {
// If the function exists, call it:
if (typeof special_func == "function") {
special_func.apply(thisObject, params);
}
};
jsCode.setAttribute('src', 'http://path/to/external/file.js');
document.body.appendChild(jsCode);
})();

http://path/to/external/file.js:

// Declare / Define special_func
var special_func = (function(){
var local_variable = "password";
var another_local_title = "Expected title";

//Return a function, which will be stored in the `special_func` variable
return function(string, title){ //Public function
if (title == another_local_title) return local_variable;
}
})();

关于javascript - 如何将参数传递给服务器端书签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7903318/

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