关于 Chrome 37+ 中 showModalDialog 被停用的临时解决方案

升级到 Chrome 37 正式版后,发现前端应用中所有使用 window.showModalDialog 的地方都抛了异常,调试发现 window 对象下找不到 showModalDialog 方法。原来,Chrome在许久以前就在控制台上提示使用 window.open 加跨window的 postMessage 方法来替代。终于在Chrome 37正式版暴发了,Chrome全面禁用了 showModalDialog 这个API方法,十分坑嗲。

值得庆幸的是,Google公司考虑到尚有许多企业级应用对此API有严重依赖,一时不容易做出完全调整,于是提供我们一个“修改Chrome策略(EnableDeprecatedWebPlatformFeatures)”以使之工作的临时解决方案,或者说是缓冲方案。之所以是说是临时解决方案,主要是基于两点:一是该API最终还是将寿终正寝,被完全删除 (大概时间是到2014-04-30);二是方案本身并非面对普通用户,具有一定的专业性。因此,终极解决方案还是使用 window.open 加 window.postMessage 替代。或者更好的办法是使用 Div 封装的 Dialog。

现在我们分别针对 Window 系统和 Linux 系统来详细描述一下该临时方案。

一、Window 系统 —— 编辑“组策略”,增加 Chrome 策略配置

这里,因为我的系统是 Window7, 所以我们就以 Window 7 为例来说明。

  1. 运行“组策略编辑器”

通过“开始”菜单或“Win + R”打开“运行”窗口,并输入 “gpedit.msc” 打开系统的“组策略编辑器”。

Run

  1. 增加 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 配置。)

Local Group Policy Editor

Add Administrative Template

  1. 配置 “EnableDeprecatedWebPlatformFeatures” 策略项

展开“Classic Administrative Templates(ADM)(经典管理模板)- Google - Chrome”,在右侧列表中找到“Enable deprecated web platform features (启用过时的Web平台功能)"。

Enable deprecated web platform features

右击,选择“Edit(编辑)”。

Edit Context Menu

在“Enable deprecated web platform features (启用过时的Web平台功能)"对话框中选择“Enable(启用)”。

Enable deprecated web platform features

在“Options(选项)”框中点击“Show(显示)”按钮,输入“ShowModalDialog_EffectiveUntil20150430”(表示设置有效期到2015年4月30日)。

Show Contents

二、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/
  1. 创建策略配置目录,并配置“写”权限
$ 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
  1. 创建策略文件“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"]
}
  1. 拷贝“chrome.json”到“/etc/opt/chrome/policies/managed”目录:
$ cp chrome.json /etc/opt/chrome/policies/managed

三、查看结果

在 Chrome 中输入“chrome://policies”.

Chrome Policies

四、参考

  1. Dev.Opera: https://dev.opera.com/blog/showmodaldialog/
  2. Blink-dev: https://groups.google.com/a/chromium.org/forum/#!msg/blink-dev/xh9fPX0ijqk/ixHZCOH6GLgJ
  3. Set Chrome policies for devices: https://support.google.com/chrome/a/answer/187202?hl=en&ref_topic=4386995

五、附件

Policy Templates

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值