1

我是 Greasemonkey 脚本的新手,想使用当前 URL 本地化页面或脚本中的链接。

例如,使用类似的链接http://en1.server.com/,捕获en1零件。

目前该脚本使用:

// @include     /^http://en[0-9].forgeofempires.com/game/index.*$/

(Greasemonkey 的正则表达式 @include 语法)

在脚本下面有:

swfobject.embedSWF("http://cdn.en.forgeofempires.com/swf/Preloader.swf?1358930484", "content", "100%", "100%", swfVersionStr, xiSwfUrlStr, flashvars, params, attributes);


但是,如果有人在匈牙利、法国或瑞典服务器上玩这个游戏,他必须手动修改脚本才能在 Linux 下正常运行游戏。

我想把脚本改成这样:

// @include     /^http://*[0-9]\.server\.com/game/index.*$/
var url = window.location.href;
var loc = "remove everything but url prefixe"
swfobject.embedSWF("http://cdn.(+loc+).forgeofempires.com/swf/Preloader.swf?1358930484", "content", "100%", "100%", swfVersionStr, xiSwfUrlStr, flashvars, params, attributes);

但我不知道如何清理 URL,只保留 en、sw、it、fr 或其他本地化前缀。

我正在尝试修改此脚本:  https ://userscripts.org/scripts/show/157358

4

1 回答 1

0

更新更新问题:

关键是只获取主机名,location.hostname然后使用正则表达式replace提取子域并去除任何尾随数字。

因此该脚本将变为:

// ==UserScript==
// @name        Forge of Empires - Enable FoE to run with Adobe Flash 11.2
// @namespace   http://userscripts.org
// @description Forge of Empires, after the update on Jan 23rd 2013., requires that Adobe Flash Player V11.3 is installed on the system. This presents the problem mostly for Linux users, becouse only version 11.2 is available on Linux. This scripts enables game to run on Adobe Flash Player V11.2.
// @include     /^http://.+?\.forgeofempires\.com/game/index.*$/
// @grant       none
// @version     2
// ==/UserScript==

var swfVersionStr   = "11.2";
var baseHost        = location.hostname;
var subdomain       = baseHost.replace (/^(.+?)\d?\.forgeofempires\.com$/i, "$1");

swfobject.embedSWF (
    "http://cdn." + subdomain + ".forgeofempires.com/swf/Preloader.swf?1358930484", 
    "content", 
    "100%", "100%", 
    swfVersionStr, 
    xiSwfUrlStr, 
    flashvars, 
    params, 
    attributes
);
swfobject.addDomLoadEvent (createFullBrowserFlash);
于 2013-02-07T05:05:54.610 回答