简体   繁体   English

防止 Gmail 关闭/保持可见

[英]Preventing Gmail From Closing / Keeping It Visible

Disclaimer before the uppity types start in: This is not to be deployed "in the wild".在自负类型开始之前的免责声明:这不是“在野外”部署的。 This is for local, personal use by a Chrome add-on only.这仅供 Chrome 插件在本地个人使用。 I am not trying trick visitors to my sites or do anything else unsavory.不是在试图欺骗我网站的访问者或做任何其他令人讨厌的事情。 I've seen a bit of chastising of those wanting to hook onbeforeunload.我已经看到了一些对那些想要在 onbeforeunload 上钩的人的批评。

Some Background一些背景

Far and away my biggest gripe with Chrome (at least on Mac OS) is its tab close buttons which, when I have several tabs open (as I usually do), results in my frequently closing tabs unintentionally as the tabs themselves become fairly small and thus the click target area that isn't covered by the tab close button is very small.毫无疑问,我对 Chrome 的最大抱怨(至少在 Mac OS 上)是它的标签关闭按钮,当我打开多个标签时(就像我通常做的那样),由于标签本身变得相当小而且我经常无意中关闭标签因此标签关闭按钮未覆盖的点击目标区域非常小。 On certain tabs with news stories, blog posts, documentation, etc. it's annoying but not a huge inconvenience.在某些带有新闻故事、博客文章、文档等的标签上,这很烦人,但不是很大的不便。 I just CMD+T to reopen the tab and there's no real harm done.我只是 CMD+T 重新打开选项卡,并没有造成真正的伤害。 However, certain tabs, in particular Gmail, have definite downsides to being closed.但是,某些选项卡,特别是 Gmail,在关闭时具有明显的缺点。 I frequently have one or more chat dialogs with coworkers open and reopening a tab does not restore the chat dialog, and initiating a new chat loses me the current chat history (yes, it's saved, but it's not conveniently accessible by scrolling backwards in context).我经常与同事打开一个或多个聊天对话框并重新打开一个选项卡不会恢复聊天对话框,并且启动新的聊天会使我失去当前的聊天历史记录(是的,它已保存,但在上下文中向后滚动不方便访问) . There are a handful of requests for Google to add the option to simply remove tab close buttons (I prefer to use CMD+W myself), but I'm not holding my breath on that.谷歌有一些要求添加选项以简单地删除标签关闭按钮(我自己更喜欢使用 CMD+W),但我并没有屏住呼吸。

A Partial Solution部分解决方案

A while back a friend pointed me towards the dotJS Chrome add-on which allows per-domain custom JavaScript execution, akin to GreaseMonkey, but slightly different.不久前,一位朋友向我介绍了dotJS Chrome 插件,它允许按域自定义 JavaScript 执行,类似于 GreaseMonkey,但略有不同。 At any rate, it gave me a means to "fix" issues/wants on a number of sites I visit regularly, and I've found it to be very helpful thus far.无论如何,它为我提供了一种在我经常访问的许多网站上“修复”问题/需求的方法,而且我发现它到目前为止非常有帮助。 The other day it occurred to me that I could probably keep my Gmail, et al tab(s) open with a bit of JavaScript.前几天我突然想到,我可以用一点 JavaScript 打开我的 Gmail 等选项卡。 I threw together a small script that based on a regular expression would prompt you before closing a tab.我拼凑了一个基于正则表达式的小脚本,它会在关闭选项卡之前提示您。 The gist of the code is as follows:代码的要点如下:

var unloadHandler = function(e) {
    if (/(mail.google.com|google.com\/reader|gmail.com)/.test(location)) {
        return 'Are you sure you want to close: ' + location.host;
    }
};

window.onbeforeunload = unloadHandler;

Lo and behold, this did the trick for most of the sites I tried it on;瞧,这对我尝试过的大多数网站都起到了作用; except one: Gmail.除了一个:Gmail。 Let me correct that: it worked, insofar as it prompted me to confirm the closing of the Gmail tab, and if I opted to not close it, it would keep the tab open, but before the dialog prompted me, the page had gone completely white.让我更正一下:它起作用了,因为它提示我确认关闭 Gmail 选项卡,如果我选择不关闭它,它会保持选项卡打开,但在对话框提示我之前,页面已经完全消失白色的。 Element inspector shows that the markup is all still there (so far as I can tell), and the styles on the elements shouldn't be hiding things (ie display: none; visibility: hidden; etc) and the elements' positioning is still correct (eg they're within the viewable area).元素检查器显示标记仍然存在(据我所知),元素上的 styles 不应该隐藏东西(即显示:无;可见性:隐藏;等)并且元素的定位仍然正确(例如,它们在可视区域内)。 I went through one-by-one removing elements to see if anything was obscuring the Gmail interface, but was never able to reveal it on screen.我逐个删除元素以查看是否有任何东西遮挡了 Gmail 界面,但始终无法在屏幕上显示出来。 I cannot for the life of me figure out what's going on.我一生都无法弄清楚发生了什么。 I'm not sure if Gmail is hooked into some event I'm unaware of (an on*before*beforeunload?), or if Google's browser is doing something special with their Gmail page, or what is causing the strange behavior.我不确定 Gmail 是否与我不知道的某些事件有关(on*before*beforeunload?),或者 Google 的浏览器是否对他们的 Gmail 页面做了一些特殊的事情,或者是什么导致了奇怪的行为。 Google Reader is unaffected by this oddity (I can prevent close and retain the contents of the page) as are all of the other sites I've tested with.谷歌阅读器不受这种奇怪的影响(我可以防止关闭并保留页面的内容),就像我测试过的所有其他网站一样。

Does anyone know what might be causing this issue?有谁知道可能导致此问题的原因?

For the record, I am running the following: Mac OS X 10.6.5, Google Chrome 10.0.648.205, and dotJS 1.3.作为记录,我正在运行以下命令:Mac OS X 10.6.5、Google Chrome 10.0.648.205 和 dotJS 1.3。

I appreciate any feedback, but I am not looking for solutions that involve: pinning tabs, changing my workflow/usage (eg not selecting tabs with the mouse), etc. I really want to figure out what, specifically, Gmail (or perhaps Chrome?) is doing that is sabotaging my efforts here.我感谢任何反馈,但我不是在寻找涉及以下内容的解决方案:固定选项卡、更改我的工作流程/用法(例如,不使用鼠标选择选项卡)等。我真的很想弄清楚 Gmail(或者可能是 Chrome ?) 这样做是在破坏我在这里的努力。 Thanks in advance.提前致谢。

I think Gmail's own code causes this behavior.我认为 Gmail 自己的代码会导致这种行为。 I can reproduce your problem (on linux machine) with subscribing for this event from the console with this function通过使用此 function 从控制台订阅此事件,我可以重现您的问题(在 linux 机器上)

window.onbeforeunload = function(e) {
    return "Hey what\'s wrong with you?!";
};

After it I've started the developer tool profiler, the last call after closing the window (and choose the 'stay on this page' answer for the question) is a removeChild function call, which removes some content from the <iframe id="canvas_frame" />.之后我启动了开发工具分析器,关闭 window 后的最后一个调用(并选择“留在此页面上”的问题答案)是 removeChild function 调用,它从 <iframe id=" canvas_frame" />。 So the content elements are not there anymore.所以内容元素不再存在。

function Fc(b) {
    return b && b.parentNode ? b.parentNode.removeChild(b) : m
}

I've found some 'traces' in the obfuscated code for subscribing for the beforeunload event, but it's hard to be sure:)我在订阅 beforeunload 事件的混淆代码中发现了一些“痕迹”,但很难确定:)

function It(b, a) {
    this.Qc = jCa++;
    this.ea = b;
    this.ka = new J(this);
    this.Qa = a;
    this.Ka = [];
    this.Za = !1;
    this.ka.ya(this.ea, "unload", this.Da);
    this.ka.ya(this.ea, "beforeunload", this.ab);
    Ypa(Zd(a), this);
    this.ia()
}

I tried to reproduce this behavior in Firefox but I think google ships different javascript codes for different browsers so I can't reproduce it.我试图在 Firefox 中重现这种行为,但我认为谷歌为不同的浏览器提供了不同的 javascript 代码,所以我无法重现它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM