升级到 Chrome 37 正式版后,发现前端应用中所有使用 window.showModalDialog 的地方都抛了异常,调试发现 window 对象下找不到 showModalDialog 方法。原来,Chrome在许久以前就在控制台上提示使用 window.open 加跨window的 postMessage 方法来替代。终于在Chrome 37正式版暴发了,Chrome全面禁用了 showModalDialog 这个API方法,十分坑嗲。
值得庆幸的是,Google公司考虑到尚有许多企业级应用对此API有严重依赖,一时不容易做出完全调整,于是提供我们一个“修改Chrome策略(EnableDeprecatedWebPlatformFeatures)”以使之工作的临时解决方案,或者说是缓冲方案。之所以是说是临时解决方案,主要是基于两点:
一是该API最终还是将寿终正寝,被完全删除 (google工程师说是到May 2015 )(参考http://blog.chromium.org/2014/07/disabling-showmodaldialog.html);
二是方案本身并非面对普通用户,具有一定的专业性。因此,终极解决方案还是使用 window.open 加 window.postMessage 替代。或者更好的办法是使用 Div 封装的 Dialog。
现在我们分别针对 Window 系统和 Linux 系统来详细描述一下该临时方案。
一、Window 系统 —— 编辑“组策略”,增加 Chrome 策略配置
这里,因为我的系统是 Window7, 所以我们就以 Window 7 为例来说明。
- 运行“组策略编辑器”
通过“开始”菜单或“Win + R”打开“运行”窗口,并输入 “gpedit.msc” 打开系统的“组策略编辑器”。
- 增加 Chrome 策略
右击“Local Computer Policy(本地计算机策略) - Computer Configurateion(计算机配置) - Administrative Template(管理模板)”选择“Add/Remove Template...(增加/删除模板)”打开“Add/Remove Template (增加/删除模板对话框)”,点击增加,选择(刚刚下载的组策略安装包)附件中“policy_templates/windows/adm/en-US”下的 chrom.adm 并确定。
(注意: en-US 为操作系统对应的 Locale 配置。)
- 配置 “EnableDeprecatedWebPlatformFeatures” 策略项
展开“Classic Administrative Templates(ADM)(经典管理模板)- Google - Chrome”,在右侧列表中找到“Enable deprecated web platform features (启用过时的Web平台功能)"。
右击,选择“Edit(编辑)”。
在“Enable deprecated web platform features (启用过时的Web平台功能)"对话框中选择“Enable(启用)”。
在“Options(选项)”框中点击“Show(显示)”按钮,输入“ShowModalDialog_EffectiveUntil20150430”(表示设置有效期到2015年4月30日)。
二、Linux 系统 —— 增加Chrome策略配置文件
这里,我们以Ubuntu为例进行说明。
Policy configuration files live under /etc/chromium for Chromium, and under
/etc/opt/chrome for Google Chrome. There are two sets of policies kept in
these directories:
one set that is required and mandated by an administrator, and one set that
is recommended for users but not required.
These two sets live at:
/etc/opt/chrome/policies/managed/
/etc/opt/chrome/policies/recommended/
- 创建策略配置目录,并配置“写”权限
$ sudo -i
$ mkdir /etc/opt/chrome/policies
$ mkdir /etc/opt/chrome/policies/managed
$ mkdir /etc/opt/chrome/policies/recommended
$ chmod -w /etc/opt/chrome/policies/managed
- 创建策略文件“chrome.json”(其它名字好像也行,没试过),写入以下内容
// Policy template for Linux.
{
// Enable deprecated web platform features
//-------------------------------------------------------------------------
// Specify a list of deprecated web platform features to re-enable. This
// policy gives administrators the ability to re-enable deprecated web
// platform features for a limited time. Features are identified by a string
// tag and the features corresponding to the tags included in the list
// specified by this policy will get re-enabled. The following tags are
// currently defined: - ShowModalDialog_EffectiveUntil20150430 If this policy
// is left not set or the list is empty, all deprecated web platform features
// will remain disabled.
"EnableDeprecatedWebPlatformFeatures": ["ShowModalDialog_EffectiveUntil20150430"]
}
- 拷贝“chrome.json”到“/etc/opt/chrome/policies/managed”目录:
$ cp chrome.json /etc/opt/chrome/policies/managed
三、查看结果
在 Chrome 中输入“chrome://policies”.