我是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
答案 0 :(得分: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);