-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Few remarks on the implement of GM_xmlhttpRequest
#104
Comments
Thanks for your interest in Your first two points are now implemented in 90ceb88. However, I shall keep using the For the third point, I shall add it to the dev plan and implement it soon. For the fourth point, it is not an easy task for chromium based browsers, so it has lower priority in my plan. A possible implement requires CDP. Everyone is welcome to contribute to this part. Please refer to the following codes if you are interested: ChromeXt/app/src/main/java/org/matrix/chromext/devtools/WebSocketClient.kt Lines 70 to 76 in 90ceb88
For the fifth point, I personally think that it is the responsibility of the UserScript to handle the relevant Cookie headers. This is simply because that my implement of |
关于第一点,不光 let headers_options_key = [
"Accept-Charset",
"Accept-Encoding",
"Access-Control-Request-Headers",
"Access-Control-Request-Method",
"Connection",
"Content-Length",
"Cookie",
"Cookie2",
"Date",
"DNT",
"Expect",
"Host",
"Keep-Alive",
"Origin",
"Referer",
"TE",
"Trailer",
"Transfer-Encoding",
"Upgrade",
"User-Agent",
"Via",
] |
感谢修复,待会儿会去试一下最新版 |
There is a chroimum bug tracker for the |
搜集了一下,我的脚本目前使用的 Accept
Authorization
Accept-Encoding
Accept-Language
Content-Type
Host
Origin
Pragma
Referer
x-csrf-token
X-Requested-With 另外关于这个有些建议90ceb88 |
Now No worry about the upper or lower spelling cases of the header keys, the Headers API of JavaScript can take care of them automatically. |
Could you give an exmaplar URL for the above bug? Maybe you missed a |
https://up.woozooo.com/mlogin.php |
I cannot reproduce it using curl -v 'https://up.woozooo.com/mlogin.php' --data-raw 'task=3&uid=jingmatrix%40gmail.com&pwd=test&setSessionId=&setSig=&setScene=&setToken=&formhash=ab2489d6' The Java implement of |
The If |
It is wired about the redirection, because I think when you specify |
很合理,但是如果resolve的话兼容性会更好一些
|
In the commit aba8e9d, I throw the parsing error out. Please tell me if this solution is acceptable for you. I think that unless you are using the async version Also, I implemented the basic APIs of |
GM_xmlhttpRequest
For your fifth point, I found a way to implement it in Now the |
// 用来获取用户是否登录的Cookie,该Cookie是HttpOnly,document.cookie无法获取到
function getCookie(cookieName) {
return new Promise((resolve) => {
GM_cookie.list({ name: cookieName }, function (cookies, error) {
if (error) {
resolve(null);
} else {
if (cookies.length == 0) {
resolve(null);
} else {
resolve(cookies[0].value);
}
}
});
});
}
await getCookie("userLogin") |
我又找到一个关于 GM_xmlhttpRequest({
url:"https://tieba.baidu.com/f/search/res?isnew=1&kw=%C4%E6%CB%AE%BA%AE%CA%D6%D3%CE&qw=%CE%E8%D1%F4%B3%C7&un=&rn=10&pn=0&sd=&ed=&sm=1",
method:"get",
headers: {
Referer: "https://tieba.baidu.com/f?ie=utf-8&kw=%E9%80%86%E6%B0%B4%E5%AF%92%E6%89%8B%E6%B8%B8",
Host: "tieba.baidu.com",
Accept:
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
},
responseType: "html",
onload:(resp)=>{console.log(resp)}
}) 在
|
Thanks for the feedbacks!
|
关于第2点,我在
// 我设置的cookie
headers: {
"user-agent":".....",
cookie: "github_test=1;",
} 发出去的Cookie会变成 |
|
对,是跨域请求,修改了 |
Now |
我尝试了一下最新的https://github.com/JingMatrix/ChromeXt/actions/runs/6228644262,似乎有严重的问题, |
Cannot reproduce your issue, in my devices, GM_xmlhttpRequest({
url: "https://bbs.binmt.cc/k_misign-sign.html?operation=qiandao&format=button&formhash=TESTHASHVALUE&inajax=1&ajaxtarget=midaben_sign",
onload: (r)=>console.log(r.response),
onerror: (r)=>console.log(r),
timeout: 5000,
forceCORS: true
}) returns a response with |
使用这个仍是未触发, QQ2023919-162632-HD.mp4 |
The re-implement of |
@WhiteSevs Did you succeed to run your script with the latest commits of |
ok,现在基本上除了那些需要跨域登录的操作都没问题了 |
CORS login should be working now. Did you still fail to do that? |
If you receive the |
在 |
Currently in |
登录👇 // token可以在www.z4a.net页面HTML中搜索到 => PF.obj.config.auth_token
GM_xmlhttpRequest({
url:"https://www.z4a.net/login",
method:"POST",
redirect: "manual",
data: `login-subject=${user}&password=${pwd}&auth_token=${xxxxx}`,
headers: {
"User-Agent": GM_bridge.Utils.getRandomPCUA(),
},
onload:(r)=>{console.log(r)},
onerror:(r)=>{console.log(r)}
}) 上传图片👇, let form = new FormData();
form.append("type", "file");
form.append("action", "upload");
form.append("timestamp", new Date().getTime());
form.append("auth_token", auth_token);
form.append("nsfw", 0);
form.append("source", imageFile);
GM_xmlhttpRequest({
url: `https://www.z4a.net/json`,
method: "POST",
data: form,
async: false,
headers: {
Accept: "application/json",
"User-Agent": utils.getRandomPCUA(),
Referer: `https://www.z4a.net/`,
Origin: "https://www.z4a.net",
},
onload:(r)=>{console.log(r)},
onerror:(r)=>{console.log(r)}
}) |
Okay, I see. That is because I didn't process |
对,但是上传的前一个步骤是先登录,那个步骤有重定向Cookie问题 |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
@WhiteSevs I have tested another website |
@WhiteSevs Did you try the cookie login again? |
|
That is because the default value for |
Do you need the response for your script? If not, you can safely ignore it for a while. |
是的,我脚本中有段是根据这个responseText内容来判断是否已成功登录,这个报错很奇怪,我是用的登录,并没有使用上传 |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
I have changed the default value of |
奇怪,最新的版本无法进行图床登录,上个版本我测试了下还可以进行登录且上传图片 |
Cannot reproduce on my phone. I tested with the following code, and it worked as expected. let url = "https://imgse.com/";
async function get_token() {
const html = await GM_bridge.GM_xmlhttpRequest({ url, forceCORS: true });
return html.match(/PF.obj.config.auth_token = "(\w+)"/)[1];
}
function login(
auth_token = PF.obj.config.auth_token,
redirect = "follow",
nocache = true
) {
const form = new FormData();
form.append("login-subject", user);
form.append("password", pwd);
form.append("auth_token", auth_token);
return GM_bridge.GM_xmlhttpRequest({
url: url + "login",
method: "POST",
redirect,
nocache,
onredirect: (xhr) => console.log(xhr),
data: form,
onload: (xhr) => console.log(xhr.status),
});
}
function createForm() {
if (document.querySelector("#chromext")) return;
const input = document.createElement("input");
input.setAttribute("type", "file");
input.setAttribute("accept", "image/*");
input.id = "chromext";
document.body.prepend(input);
return input;
}
function upload(auth_token = PF.obj.config.auth_token, putFile = true) {
input = document.querySelector("#chromext");
if (putFile && typeof input.files[0] == "undefined") {
console.warn("No file chosen yet");
}
const form = new FormData();
form.append("type", "file");
form.append("action", "upload");
form.append("timestamp", new Date().getTime());
if (putFile) form.append("source", input.files[0]);
form.append("auth_token", auth_token);
form.append("nsfw", 0);
GM_bridge.GM_xmlhttpRequest({
responseType: "json",
url: url + "json",
method: "POST",
data: form,
timeout: 30000,
onload: (xhr) => console.log(xhr.response),
});
}
function test() {
const promise = get_token().then((token) => {
console.log("Auth_token:", token);
login(token);
});
createForm();
return promise;
} You run it with |
|
Even with This will not be considered as a bug. If you are Okay with that, please close the issue. |
OK,删除脚本后重新安装了下,第一次登录失败,第二次登录成功且上传图片完毕,应该是这个图床网站的问题 |
fetch
来实现GM_xmlhttpRequest
,因为有时候同源请求需要设置headers
的User-Agent
时,也是属于跨域了,使用fetch
的话不会生效该User-Agent
;method
为GET
时,如果details
中存在data
键,那么设置把method
为POST
,不然https://github.com/JingMatrix/ChromeXt/blob/master/app/src/main/assets/GM.js的第381
行会报错GM_cookie
未实现,希望可以在脚本环境中删除该API,在·TamperMonkey·中的GM_cookie
是ojbect
类型;iframe
内注入?webview
可以hook
接口shouldInterceptRequest
,参数WebResourceRequest
对它返回的html进行修改加入js;GM_xmlhttpRequest
似乎并没有对请求自动加入Cookie
,比如我对一个api进行请求登录,返回的headers
里有Cookie
信息,后续的同域名请求并没有把Cookie
加进去;The text was updated successfully, but these errors were encountered: