Greasemonkey 中使用 GM_xmlhttpRequest 如何在 https 页面中加载 http 资源?

之前写过一个ff下的gm脚本,功能是在豆瓣读书页面加载图书馆的馆藏信息,但是随着豆瓣全站https,脚本失效,尝试更改,但是仍然无法使用,请大家看看这个问题改如何解决。

目前的代码段

// ==UserScript==
// @name           douban_opac
// @include        https://book.douban.com/subject/*
// @include        https://book.douban.com/isbn/*
// @grant          GM_xmlhttpRequest
// ==/UserScript==
$(document).ready(function(){
    if ((window.location.href).indexOf("book.douban") > 0 ) {
        var title = $('h1').text();    
        console.log(title);
        $("#info .pl").each(function(i){
        if ($(this).text() == 'ISBN:'){
              var isbn = $(this)[0].nextSibling.nodeValue;
              isbn = isbn.substr(1,13);           
              GM_xmlhttpRequest({ //获取列表
                  method : "GET",           
                headers: {"Accept": "application/json"},
                url : "http://localhost/getLOC.php?isbn="+isbn+"&title="+title+"&callback=?",
                  onload : function (response) {
                     console.log(response.Reponsetext);                
                }
             });
        }
        });    
    }
});

加载脚本之后,在浏览器开发工具的console中,查看不到任何log记录。如果是不使用GM_xmlhttpRequest,则在console中有title的记录。

怀疑是因为豆瓣采用了https,而使浏览器无法加载http的资源。不知道是不是这样,请教该如何解决此问题?

阅读 5.7k
2 个回答

解决了此问题。

问题不在于GM_xmlhttpRequest中无法加载http,而是在脚本中使用了jquery,而GM中并没有require jquery,只要在userscipt的metadata中
@require http://libs.baidu.com/jquery/2.0.0/jquery.js
加载jquery就可以执行了。

还没有查清楚,为什么之前不用require jquery类库,而现在需要了。这个可能跟GM的机制有关,脚本文件只能使用metadata中定义的文件,不能直接使用网页上的js文件。

不知道你为什么没有log,不过我知道
console.log(response.Reponsetext);
你的responseText 写错了。

如果正常,你的log应该会返回undefined。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进