Last active
October 14, 2022 14:16
-
-
Save cafe4it/90d8cd8e8003b22639ad1a1f9a0e207c to your computer and use it in GitHub Desktop.
[Bookmarklet] for Chrome Extension [Super Simple Highlighter] printing color
This file contains hidden or 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
var importScript = (function(oHead) { | |
function loadError(oError) { | |
throw new URIError("The script " + oError.target.src + " is not accessible."); | |
} | |
return function(sSrc, fOnload) { | |
var oScript = document.createElement("script"); | |
oScript.type = "text\/javascript"; | |
oScript.onerror = loadError; | |
if (fOnload) { | |
oScript.onload = fOnload; | |
} | |
oHead.appendChild(oScript); | |
oScript.src = sSrc; | |
} | |
})(document.head || document.getElementsByTagName("head")[0]); | |
importScript("https://cdn.rawgit.com/cafe4it/-Bookmarklet-for-Chrome-Extension-Super-Simple-Highlighter-printing-color/master/cssParser.min.js", function() { | |
var highlightEle = document.querySelector('style[id]') | |
var headEle = document.getElementsByTagName('head')[0] | |
if (highlightEle) { | |
var ss = highlightEle.innerHTML | |
var parser = new CSSParser() | |
var objCss = parser.parse(ss, false, true) | |
var cssRules = objCss["cssRules"] | |
if (cssRules) { | |
var cssPrint = "" | |
var forceStyle = "{-webkit-print-color-adjust: exact !important;}" | |
cssRules.forEach(function(obj) { | |
if (obj["mSelectorText"]) { | |
cssPrint += (obj["mSelectorText"] + forceStyle) | |
} | |
}) | |
var printEle = document.createElement('style') | |
printEle.setAttribute("type", "text/css") | |
printEle.setAttribute("media", "print") | |
printEle.setAttribute("id", "printStyle") | |
printEle.innerHTML = cssPrint | |
var IsExistsPrintEle = document.getElementById("printStyle") | |
if (IsExistsPrintEle) { | |
headEle.removeChild(IsExistsPrintEle) | |
} | |
headEle.appendChild(printEle) | |
window.print() | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment