Skip to content

Instantly share code, notes, and snippets.

@growse
Last active February 14, 2020 23:17
Show Gist options
  • Save growse/6210956 to your computer and use it in GitHub Desktop.
Save growse/6210956 to your computer and use it in GitHub Desktop.
Bookmarklet that lets you apply arbitrary google fonts to jQuery selectors.
var bkmkltFontChooser = function () {
return {
v: "1.10.2",
jQueryDone: false,
webfontDone: false,
fontChooserLoaded: false,
loadjQuery: function () {
if (window.jQuery === undefined || window.jQuery.fn.jquery < bkmkltFontChooser.v) {
console.log("Loading jQuery");
var script = document.createElement("script");
script.src = ('https:' === document.location.protocol ? 'https' : 'http') +
"://ajax.googleapis.com/ajax/libs/jquery/" + bkmkltFontChooser.v + "/jquery.min.js";
script.onload = script.onreadystatechange = function () {
if (!bkmkltFontChooser.jQueryDone && (!this.readyState || this.readyState === "loaded" || this.readyState === "complete")) {
bkmkltFontChooser.jQueryDone = true;
}
};
document.getElementsByTagName("head")[0].appendChild(script);
} else {
bkmkltFontChooser.jQueryDone = true;
}
},
loadWebFont: function () {
if (window.WebFont === undefined) {
console.log("Loading Webfont");
var wf = document.createElement('script');
wf.src = ('https:' === document.location.protocol ? 'https' : 'http') +
'://ajax.googleapis.com/ajax/libs/webfont/1.4.7/webfont.js';
wf.type = 'text/javascript';
wf.onload = wf.onreadystatechange = function () {
if (!bkmkltFontChooser.webfontDone && (!this.readyState || this.readyState === "loaded" || this.readyState === "complete")) {
bkmkltFontChooser.webfontDone = true;
}
};
document.getElementsByTagName("head")[0].appendChild(wf);
} else {
bkmkltFontChooser.webfontDone = true;
}
},
waitForLibs: function () {
if (!bkmkltFontChooser.jQueryDone || !bkmkltFontChooser.webfontDone) {
console.log("Spin " + bkmkltFontChooser.jQueryDone + " " + bkmkltFontChooser.webfontDone);
setTimeout(bkmkltFontChooser.waitForLibs, 200);
} else {
bkmkltFontChooser.initfontChooser();
}
},
firstTimeInit: function () {
bkmkltFontChooser.loadjQuery();
bkmkltFontChooser.loadWebFont();
bkmkltFontChooser.waitForLibs();
},
setFont: function () {
var fontname = jQuery("#bkmkltFontChooser-fontname").val();
var selector = jQuery("#bkmkltFontChooser-selector").val();
console.log("Applying font "+fontname+" to "+selector);
if (fontname) {
WebFont.load({
google: {
families: [fontname]
}
});
if (selector) {
jQuery(selector).css('font-family', fontname);
}
}
},
initfontChooser: function () {
console.log("Gogogo");
window.fontChooserloaded = true;
if (jQuery("#bkmkltFontChooser").length === 0) {
jQuery("body").append("<style>#bkmkltFontChooser * {font-family: sans-serif; vertical-align: baseline; margin: 0; padding: 0; color: #000; height: auto; width: auto; line-height: normal; text-align: left;}#bkmkltFontChooser{z-index:9999999999; font-size: 12px !important; background-color:#eee;position:absolute; display:none; width: 200px; top:0; left:0;padding:5px;}#bkmkltFontChooser label{margin: 5px 0;}#bkmkltFontChooser input{border:1px solid black; border-radius:0; padding: 2px;}#bkmkltFontChooser p{margin: 5px 0;}#bkmkltFontChooser a{color: #000; float:right;}#bkmkltFontChooser a:hover{text-decoration:none;}</style>");
jQuery("body").append('<div id="bkmkltFontChooser"><form><p><label for="bkmkltFontChooser-fontname">Font Name: <input id="bkmkltFontChooser-fontname" type="text"></label></p><p><label for="bkmkltFontChooser-selector">DOM Selector: <input id="bkmkltFontChooser-selector" type="text"></label></p><p><button type="button" onclick="bkmkltFontChooser.setFont(); return false;">Go!</button><a href="javascript:jQuery("#bkmkltFontChooser").hide();">×</a></p></form></div>');
}
jQuery("#bkmkltFontChooser-selector").val("");
jQuery("#bkmkltFontChooser-fontname").val("");
jQuery("#bkmkltFontChooser").show();
jQuery("#bkmkltFontChooser-fontname").focus();
}
};
}();
(function () {
bkmkltFontChooser.firstTimeInit();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment