使用 window.location.href() 将网站重定向到本地文件夹不起作用

Redirect a Website to a local folder Using window.location.href() not working

developer.android 在我的国家不可用,所以我写了一个 greasemonkey 脚本来将我的 google 搜索重定向到我的 sdk 文件夹中的本地 android 文档。

但它没有 work.i

好像window.location.href不能 处理本地地址。

// ==UserScript==
// @name        Android Doc Redirect
// @namespace   me
// @author      me
// @description Redirect developer.android.com to local Documention
// @include     *http://developer.android.com/*
// @match        http://developer.android.com/*
// @version     1.0
// @grant       none
// ==/UserScript==

(function() {
  var url = window.location.href;
  var str = "http://developer.android.com/";
  var str2 = "file:///home/user/android-sdk-linux/docs/";
//   var str2 = "http://google.com";
  if(url.startsWith(str))
    {
      url = url.replace(str, str2);
      window.location.href = url;
    }
}())

几乎可以肯定,设置本地服务器是最好的方法,因为它可以避免浏览器中 file:// 扩展的安全问题。我建议将 node 与简单的 http 服务器一起使用。

(或使用 static serve 之类的东西)

如果您真的想禁用您的安全警告,localLink plugin that will do it, or you can use no script. There are also few other solutions here 关于禁用安全 flag/limit。