在 Google Chrome 中,Google Docs 可以从 javascript 控制页眉和页脚。有谁知道这是如何实现的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9672739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Google Chrome, Google Docs can control headers and footers from javascript. Does anybody know how this is achieved?
提问by Sebastian Ferreyra
That's basically it. Somehow, the javascript in Google Docs can turn off the default headers and footers showing the URL, date, page numbering, etc.
基本上就是这样。不知何故,Google Docs 中的 javascript 可以关闭显示 URL、日期、页码等的默认页眉和页脚。
This only works in Google Chrome. When Google Docs is running from another browser it will print the headers/footers unless removed manually through the print configuration dialog. In Safari, it seems to generate a PDF server-side which of course will print outside of the browser.
这只适用于谷歌浏览器。当 Google Docs 从另一个浏览器运行时,它将打印页眉/页脚,除非通过打印配置对话框手动删除。在 Safari 中,它似乎会生成一个 PDF 服务器端,当然会在浏览器之外打印。
I've searched around the web and have found nothing on how this is done. The javascript in the page is of course minified and obfuscated so it's difficult to get any insights from there.
我在网上搜索过,但没有找到任何关于如何完成的信息。页面中的 javascript 当然被缩小和混淆,因此很难从那里获得任何见解。
So before I'm forced to dive into that spaghetti, I'd like to know if anybody has any ideas of how this is done.
所以在我被迫深入研究意大利面之前,我想知道是否有人对如何做到这一点有任何想法。
回答by Sebastian Ferreyra
After delving into some source code I found on a web page that has also resolved the issue (not Google Docs), the secret is the following CSS:
在深入研究了我在也解决了该问题的网页(不是 Google Docs)上找到的一些源代码后,秘诀是以下 CSS:
@page
{
margin:0;
}
This only works on Chrome, and perhaps Opera though I have to verify if the latter is true as it's printing the background color by default while chrome prints with a white background and the colored background in my page might be just obscuring the header/footer text.
这仅适用于 Chrome,也许 Opera 虽然我必须验证后者是否正确,因为它默认打印背景颜色,而 chrome 打印白色背景,我页面中的彩色背景可能只是模糊了页眉/页脚文本.
Other browsers give differing results:
其他浏览器给出不同的结果:
- Firefox and Safari ignore the margins apparently and print the headers/footers anyway.
- IE9 makes a mess of things and the contents print overlapped with the header/footer text. Poor browser always gets all the heat...
- Firefox 和 Safari 显然忽略了边距,无论如何都会打印页眉/页脚。
- IE9 把事情弄得一团糟,内容打印与页眉/页脚文本重叠。糟糕的浏览器总是很热...
In conclusion, combining this with silent printing coming out in Chrome 18 kiosk mode will make for some interesting functionality, such as mail merge capabilities right from the browser though I still have to research how secure kiosk mode is though, as navigating to a malicious page runs the risk of exhausting printer ink and paper.
总之,将此与 Chrome 18 kiosk 模式中的静默打印相结合将产生一些有趣的功能,例如直接从浏览器中的邮件合并功能,尽管我仍然需要研究 kiosk 模式的安全性,如导航到恶意页面冒着耗尽打印机墨水和纸张的风险。
Navigation should be restricted to a URL white-list in this case.
在这种情况下,导航应仅限于 URL 白名单。