【问题标题】:Load a javascript resource from an external source, when it's blocked with a 403 status从外部源加载 javascript 资源,当它被 403 状态阻止时
【发布时间】:2013-01-16 04:13:21
【问题描述】:

由于某种原因,我的 ISP 阻止了以下 URL:

http://assets.tumblr.com/javascript/prototype_and_effects.js

Chrome 控制台状态:

Failed to load resource: the server responded with a status of 403 (URLBlocked)

结果是我无法正确使用 Tumblr,因为很多功能都依赖于这个脚本。我已经联系了我的 ISP 并要求他们停止阻止此 URL,但同时我想对此做点什么。

如何从外部加载此资源?它可以是书签解决方案、用户脚本/Greasemonkey 或任何其他您能想到的东西。

【问题讨论】:

  • 你确定那是你的 ISP 但不是任何代理服务器吗?
  • @AlvinWong 当我联系他们时,他们告诉我他们会调查此事,但现在你提到代理,我意识到我有时会使用代理。我会检查的。尽管如此,我仍然想知道如何完成从外部源加载资源。
  • 使用自定义代理服务器,但我不会将其作为答案发布。

标签: javascript download greasemonkey userscripts


【解决方案1】:

A 403 status 表示 服务器 (assets.tumblr.com) 阻止了请求,而不是您的 ISP。服务器执行此操作的最常见原因是(a)因为您没有以足够的访问权限登录,和/或(b)服务器没有收到它想要的引荐来源标头和/或 cookie,和/或(c ) 请求来自服务器已列入黑名单的 IP 地址。使用代理服务器可以为某些网站触发任何或所有这些。

这意味着,如果您或您的代理服务器被阻止访问该文件,那么用户脚本将使用的注入远程 javascript 的标准方法也将被阻止。

为了解决这个问题Greasemonkey 可以从本地副本回填 javascript 文件。为此:

  1. 创建文件Insure Tumbler has prototype.user.js,如下所示。将其放置在您计算机上的 temp 文件夹之外的目录中。
  2. 下载您引用的the prototype_and_effects.js file 并将其放在同一文件夹中。您可能必须使用不同的(或不使用代理)或不同的浏览器配置文件,或其他任何东西。 (对我来说,只需右键单击前面的链接即可下载。)
  3. 使用 Greasemonkey 安装脚本。 (Firefox:File -> Open (CtrlO) 会起作用。)
  4. 脚本测试PrototypeEffect 库,如果缺少一个,则在本地加载它们。 可能需要更多才能让 Tumblr 再次工作,但如果是这样,那超出了这个问题的范围。
  5. 在 Firefox+Greasemonkey 中工作。应该在 Chrome+Tampermonkey 中工作(未测试),在不支持 @resource 的地方工作,例如直接 Chrome。


// ==UserScript==
// @name     _Backfill Prototype and Effect libraries on Tumblr pages
// @match    http://tumblr.com/*
// @match    http://www.tumblr.com/*
// @match    https://tumblr.com/*
// @match    https://www.tumblr.com/*
// @resource PandE_src  prototype_and_effects.js
// @grant    GM_getResourceText
// ==/UserScript==

//-- Does this page load prototype_and_effects.js?
var protoScriptNode = document.querySelector ("script[src*='prototype_and_effects']");
if (protoScriptNode) {
    //console.log ("Page uses prototype_and_effects.js.");

    //-- Are Prototype and Effects loaded?
    var P   = unsafeWindow.Prototype;
    var E   = unsafeWindow.Effect;
    if (P  &&  P.Version  &&  E  &&  E.BlindDown) {
        //console.log ("Everything's loaded, no action needed.");
    }
    else {
        //console.log ("Loading prototype_and_effects.js");
        var PandE_src           = GM_getResourceText ("PandE_src");
        var scriptNode          = document.createElement ('script');
        scriptNode.type         = "text/javascript";
        scriptNode.textContent  = PandE_src;
        var targ                = document.getElementsByTagName ('head')[0];
        targ.appendChild (scriptNode);
    }
}
else {
    //-- No action needed
    //console.log ("Page doesn't use prototype_and_effects.js.");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-12
    相关资源
    最近更新 更多