javascript 更改 chrome 打印预览默认选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8454730/
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
change chrome print preview default options
提问by jaycode
In chrome print preview, under Options tab, the default is having Headers and footers ticked on. Is it not possible to set it default to off via javascript / chrome extension / anything outside telling user to do it manually?
在 chrome 打印预览中,在选项选项卡下,默认是勾选页眉和页脚。是否无法通过 javascript / chrome 扩展程序 / 外部告诉用户手动执行的任何操作将其设置为默认关闭?
OR is it possible to remove the date displayed there?
或者是否可以删除那里显示的日期?
回答by Daniel Mendel
In short, yes you can control this behavior. It is actually very bizzarre -- as appears to have been originally answered in the question "In Google Chrome, Docs Can Control Headers and Footers from Javascript?" The secret to this behavior is in the @page
CSS property*.
简而言之,是的,您可以控制这种行为。这实际上非常奇怪——正如最初在问题“在谷歌浏览器中,文档可以控制来自 Javascript 的页眉和页脚?”的答案一样,这种行为的秘诀在于@page
CSS 属性*。
If you set this exact style:
如果您设置此确切样式:
@page { margin: 0; }
Then you get the desired behavior, the headers & footers option disappears:
然后你得到了想要的行为,页眉和页脚选项消失了:
This appears to be an undocumented behavior of Google Chrome, or at least not widely known -- cursory digging in to their developer docs garnered nothing. Google uses it themselves throughout their own web-apps, so it's probably not a secret, but I couldn't find any official notes on it.
这似乎是谷歌浏览器的一种未记录的行为,或者至少没有广为人知——粗略地挖掘他们的开发人员文档一无所获。谷歌在他们自己的网络应用程序中自己使用它,所以这可能不是秘密,但我找不到任何官方说明。
Notably, a margin of 0
does not simply disable the "headers & footers" option, it actually causes it to disappear from the menu entirely ( by quickly folding ).
值得注意的是,边距0
并不会简单地禁用“页眉和页脚”选项,它实际上会导致它从菜单中完全消失(通过快速折叠)。
Experimentation reveals that other variations of this do not have the same effect. For example margin: 0; margin-left: 10cm;
does not cause this behavior.
实验表明,此的其他变体没有相同的效果。例如margin: 0; margin-left: 10cm;
不会导致此行为。
If you do choose to implement this solution, it means forfeiting the ability to control those margins through the @page
property -- you should instead be controlling them as part of a CSS print-media stylesheet. Bummer that Chrome kind of destroys the @page
property this way. That said, FF
still does not support @page
( as of now ) so it's not a perfect solution to begin with.
如果您确实选择实施此解决方案,则意味着丧失通过@page
属性控制这些边距的能力——您应该将它们作为 CSS 印刷媒体样式表的一部分进行控制。很遗憾 Chrome 以@page
这种方式破坏了财产。也就是说,FF
仍然不支持@page
(截至目前)所以它不是一个完美的解决方案。
*More information about the @page
property
*有关该@page
物业的更多信息
The @page
CSS is supposed to control the margins of the printed page, the specificationsays:
该@page
CSS应该控制打印页边距的规格说:
The page box is a rectangular region that contains two areas:
The page area. The page area includes the boxes laid out on that page. The edges of the first page area establish the rectangle that is the initial containing block of the document. The canvas background is painted within and covers the page area.
The margin area, which surrounds the page area. The page margin area is transparent.
页面框是一个包含两个区域的矩形区域:
页面区域。页面区域包括在该页面上布置的框。第一个页面区域的边缘建立矩形,它是文档的初始包含块。画布背景绘制在页面区域内并覆盖页面区域。
页边距区域,围绕页面区域。页边距区域是透明的。
Most modern browsers supportthe @page
CSS property. Firefox is the standout who doesn't, though they do fully document it in their developer resources, and their issue tracker has a ticket for thisthat goes back 10 years. The most recent action on it was today, so perhaps FF will provide @page
support soon.
大多数现代浏览器都支持的@page
CSS属性。Firefox 是最杰出的,但他们确实在开发人员资源中完整记录了它,并且他们的问题跟踪器有一张可以追溯到 10 年前的票证。最近的行动是在今天,所以也许 FF@page
很快就会提供支持。
回答by mrtsherman
Well the page is just html, so if you can execute javascript on the page then yes you can do this. I don't recall whether Chrome extensions can bind to internal pages though. Do you know how to write a Chrome Extension? If so this would be easy enough to test.
好吧,页面只是 html,所以如果您可以在页面上执行 javascript,那么是的,您可以执行此操作。我不记得 Chrome 扩展程序是否可以绑定到内部页面。您知道如何编写 Chrome 扩展程序吗?如果是这样,这将很容易测试。
document.getElementById('header-footer').checked = false;
回答by abraham
Chrome only allows you to overridecouple specific chrome://
pages and the print page is not one of them. You are also limited to injectingscripts into a couple of specific schemes.
Chrome 只允许您覆盖几个特定chrome://
页面,而打印页面不是其中之一。您也仅限于将脚本注入到几个特定的方案中。
A match pattern is essentially a URL that begins with a permitted scheme (http, https, file, or ftp)
匹配模式本质上是以允许的方案(http、https、文件或 ftp)开头的 URL
回答by user2815568
In current version of Google Chrome, Print Preview now have option to include or not header and footer, as well as define margins and include or not background colors and images. Just update your browser to the last version.
在当前版本的谷歌浏览器中,打印预览现在可以选择包含或不包含页眉和页脚,以及定义边距和包含或不包含背景颜色和图像。只需将浏览器更新到最新版本即可。
回答by user3071808
Current Version of 31 remembers setting of of whether you want it two sided, headers and footers and printing of background images and colors but far as I can tell it won't remember margin settings.
当前版本的 31 会记住是否需要双面设置、页眉和页脚以及背景图像和颜色的打印,但据我所知,它不会记住边距设置。
回答by lyuehh
It seems like that there is no way to do such thing.
似乎没有办法做这样的事情。