隐藏的 iframe 路由是可行的方法,但在这种情况下它被translate.google.com阻止。
这是确保 Firefox 拥有让您的混搭站点 ( lema.rae.es ) 满意所需的新鲜 cookie 的另一种方法:
查找一些源 HTML,当混搭站点需要新鲜的 cookie 时,这些源 HTML 存在,但在其他情况下不存在。
在这种情况下,JS 源代码function challenge
就可以了。
访问GM_xmlhttpRequest
混搭站点并测试响应。
如果 GM_xmlhttpRequest响应具有所需的数据,请根据需要对其进行解析。
完毕!
如果 GM_xmlhttpRequest响应具有“需要 cookie”源,则在弹出窗口中打开混搭站点的特殊查询:
- 由于该站点是在自己的窗口中打开的,因此不会被跨域保护措施阻止。
- 将 GM 脚本设置为也对这个特殊的 URL 进行操作。
- 对于特殊 URL,等到关键节点或文本出现,表明页面已完成加载并设置了 cookie。
- 弹出窗口完成后,它会向打开页面发送一条消息,然后自行关闭。
- 当打开的页面得到消息时,它重新GM_xmlhttp请求mashup页面。
- 解析它并完成!
这是一个完整且经过测试(在 Firefox 上)的 Greasemonkey 脚本,它将 lema.rae.es/drae/srv/search 混合到translate.google.com中。:
// ==UserScript==
// @name _GM_xmlhttpRequest that needs cookie(s)
// @include https://translate.google.com/*
// @include http://lema.rae.es/drae/srv/search?val=openedByGM
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_xmlhttpRequest
// ==/UserScript==
//--- Global variables
var mashupURL = "http://lema.rae.es/drae/srv/search?val=test";
var cookGenURL = "http://lema.rae.es/drae/srv/search?val=openedByGM";
if (location.href == cookGenURL) {
//--- May be best we can do until Greasemonkey fixes tab handling flaws.
document.title = "Close me!";
if (window.opener) {
waitForKeyElements ("i:contains(openedByGM)", closePopupIfCan);
}
}
else {
attemptMashup ();
window.addEventListener ("message", receiveCookieMessage, false);
}
//-- Just functions from here on down...
function closePopupIfCan (jNode) {
window.opener.postMessage ("Cookie(s) should be set!", "*");
window.close ();
}
function attemptMashup () {
GM_xmlhttpRequest ( {
method: "GET",
url: mashupURL,
onload: parseDictionaryResponse,
onabort: reportAJAX_Error,
onerror: reportAJAX_Error,
ontimeout: reportAJAX_Error
} );
}
function receiveCookieMessage (event) {
if (event.origin != "http://lema.rae.es") return;
console.log ("message ==> ", event.data);
/*--- Now that have cookie(s), re-attempt mashup, but need a little
settling time.
*/
setTimeout (attemptMashup, 888);
}
function parseDictionaryResponse (respObject) {
if (respObject.status != 200 && respObject.status != 304) {
reportAJAX_Error (respObject);
return;
}
/*--- If the required cookie is not present/valid, open the target page
in a temporary tab to set the cookies, then reload this page.
The test string is unique to the scraped site and is only present
when cookie(s) is/are needed.
*/
if (/function\s+challenge/i.test (respObject.responseText) ) {
var newTab = window.open (cookGenURL);
return;
}
//--- Don't use jQuery to parse this!
var parser = new DOMParser ();
var responseDoc = parser.parseFromString (
respObject.responseText, "text/html" // Firefox only, for now.
);
//--- Get site-specific payload and put in site-specific location.
var payload = responseDoc.querySelectorAll ("body > div");
$("#gt-form-c").before (payload);
}
function reportAJAX_Error (respObject) {
alert (
'Error ' + respObject.status + '! "' + respObject.statusText + '"'
);
}