Created
April 29, 2023 14:14
-
-
Save sharkpp/94bc000d5cca12135688ac0d84aaeeaf to your computer and use it in GitHub Desktop.
ブックマークレット:印刷時に@media screenを利用
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ブックマークレット:印刷時に@media screenを利用 | |
// based on https://miau.hatenablog.com/entry/20110202/1296619984 | |
(function () { | |
// スタイルシートごとのループ | |
for (var s = document.styleSheets, i = 0; i < s.length; i++) { | |
var e = s[i], | |
m = e.media; | |
if (m.mediaText == "screen") { | |
// screen の場合は印刷時も適用されるよう all に変更する | |
m.mediaText = "all" | |
} else if (m.mediaText == "print") { | |
// print の場合は印刷時に適用されないよう無効化する | |
e.disabled = true; | |
continue; | |
} | |
// スタイル定義ごとに @media が指定されているケースの対応 | |
// ※簡易版では削除しています | |
if (!e.cssRules) { | |
continue; | |
} | |
for (var j = e.cssRules.length - 1; j >= 0; j--) { | |
var r = e.cssRules[j]; | |
if (r.media) { | |
if (0<=r.media.mediaText.indexOf("screen")) { | |
r.media.mediaText = r.media.mediaText.replace(/screen/g,"all") | |
} else if (r.media.mediaText == "print") { | |
e.deleteRule(j); | |
} | |
} | |
} | |
} | |
})() | |
// ↓をブックーマークに追加 | |
// javascript:!function()%7Bfor(var e%3Ddocument.styleSheets%2Ci%3D0%3Bi<e.length%3Bi%2B%2B)%7Bvar a%3De%5Bi%5D%2Cd%3Da.media%3Bif("screen"%3D%3Dd.mediaText)d.mediaText%3D"all"%3Belse if("print"%3D%3Dd.mediaText)%7Ba.disabled%3D!0%3Bcontinue%7Dif(a.cssRules)for(var t%3Da.cssRules.length-1%3Bt>%3D0%3Bt--)%7Bvar s%3Da.cssRules%5Bt%5D%3Bs.media%26%26(0<%3Ds.media.mediaText.indexOf("screen")%3Fs.media.mediaText%3Ds.media.mediaText.replace(%2Fscreen%2Fg%2C"all")%3A"print"%3D%3Ds.media.mediaText%26%26a.deleteRule(t))%7D%7D%7D()%3Bvoid(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment