【问题标题】:Perform a GM_xmlhttpRequest before and after loading a page on Firefox在 Firefox 上加载页面之前和之后执行 GM_xmlhttpRequest
【发布时间】:2014-07-19 00:28:53
【问题描述】:

我正在度假,我酒店的互联网连接价格太高了。

在使用 http post 请求将有效的用户名/密码对发送到 http://an.internal.address/login 之前,网络流量将被阻止。

但是,它们使用户可以通过访问 http://an.internal.address/logout 来关闭连接

我想通过使用 Greasemonkey 自动执行以下操作来最大化我的 1 小时互联网票的持续时间:

  1. http post data "username=MY_USERNAME&password=MY_PASSWORD" 到http://an.internal.address/login
  2. 使用我的浏览器加载页面
  3. http 获取http://an.internal.address/logout

我使用 Firefox 30,我认为 Greasemonkey 实际上可以做到这一点。但是,我第一次尝试编写用户脚本:它可以部分工作。

似乎根本没有调用 login(),logout 也没有执行 GM_xmlhttpRequest,因为没有发出包含响应的 alert()。

// ==UserScript==
// @name        test
// @namespace   leonixyz
// @version     0.1
// @description Calls shell script once a page has been loaded or leaved
// @match       http://*/*
// @match       https://*/*
// @copyright   leonixyz
// ==/UserScript==

function login() {
    GM_xmlhttpRequest({
       method: "POST",
       url: "http://1.1.1.1/login",
       data: "username=DE14945&password=VBJEETYN&dst=&popup=true",
       onload: function(response) {
          alert(response.responseText);
       }
    });
}

function logout() {
    GM_xmlhttpRequest({
       method: "GET",
       url: "http://1.1.1.1/logout",
       onload: function(response) {
           alert(response.responseText);
       }
    });
}

window.addEventListener("unload", login);
window.addEventListener("load", logout);

【问题讨论】:

  • onloadonunload,但 onunload 还不可靠。
  • 你在度假;扔掉小玩意儿,享受离网的几天吧! ...无论如何,仅使用 Greasemonkey 就可以实现您想要的,但我们无法查看可能至关重要的细节或测试解决方案。

标签: javascript linux firefox greasemonkey userscripts


【解决方案1】:

已解决:所有 GM_functions() 都需要在标头中声明,在 @grant 元标记之后。 如果您打算在克罗地亚波雷奇附近的德尔芬酒店度假,请在接待处购买 1 小时的互联网票并享受以下脚本(有时您必须手动连接,特别是在打开新标签时,但断开连接肯定有效并节省您宝贵的连接时间):

// ==UserScript==
// @name        Laguna Wifi hack
// @namespace   leonixyz
// @version     0.1
// @description connect/disconnect automatically when browsing
// @match       http://*/*
// @match       https://*/*
// @exclude     http://1.1.1.1/*
// @copyright   leonixyz
// @grant       GM_xmlhttpRequest
// ==/UserScript==

function login() {
    GM_xmlhttpRequest({
       method: "POST",
       url: "http://1.1.1.1/login",
       data: "username=MY_USERNAME&password=MY_PASSOWRD"
    });
}

function logout() {
    GM_xmlhttpRequest({
       method: "GET",
       url: "http://1.1.1.1/logout"
    });
}

window.addEventListener("beforeunload", login);
window.addEventListener("load", logout);

【讨论】:

    猜你喜欢
    • 2015-04-07
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-27
    • 2021-08-02
    • 1970-01-01
    • 2012-10-27
    相关资源
    最近更新 更多