拦截Gmail发送邮件的AJAX请求涉及浏览器扩展开发、网络请求拦截和Gmail的内部API结构。Gmail使用复杂的JavaScript应用架构,通过AJAX请求与后端服务器通信。
// manifest.json
{
"name": "Gmail Request Interceptor",
"version": "1.0",
"manifest_version": 3,
"permissions": ["webRequest", "webRequestBlocking", "https://mail.google.com/*"],
"background": {
"service_worker": "background.js"
}
}
// background.js
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
if (details.url.includes('mail.google.com/mail/u/') &&
details.url.includes('/send') &&
details.method === 'POST') {
console.log('Intercepted Gmail send request:', details);
// 可以修改请求内容或阻止发送
// return {cancel: true}; // 阻止发送
}
return {cancel: false};
},
{urls: ["https://mail.google.com/*"]},
["blocking", "requestBody"]
);
const originalXHROpen = XMLHttpRequest.prototype.open;
const originalXHRSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.open = function(method, url) {
this._method = method;
this._url = url;
return originalXHROpen.apply(this, arguments);
};
XMLHttpRequest.prototype.send = function(body) {
if (this._url.includes('/send') && this._method === 'POST') {
console.log('Intercepted Gmail send request:', {
url: this._url,
method: this._method,
body: body
});
// 可以修改body或阻止发送
// return; // 阻止发送
}
return originalXHRSend.apply(this, arguments);
};
问题1:拦截不到请求
const originalFetch = window.fetch;
window.fetch = function(url, options) {
if (url.includes('/send') && options.method === 'POST') {
console.log('Intercepted Gmail send request (Fetch):', {url, options});
}
return originalFetch(url, options);
};
问题2:请求内容无法读取
问题3:扩展被Gmail检测到并阻止
如果目的是监控或修改发送的邮件,考虑使用Gmail官方API可能是更可靠的选择:
// 使用Gmail API需要先获取授权
gapi.client.gmail.users.messages.send({
'userId': 'me',
'resource': {
'raw': base64EncodedEmail
}
}).then(function(response) {
console.log(response);
});
这种方法需要用户授权,但更稳定且符合Gmail的使用政策。
没有搜到相关的沙龙