I am working on project where user wants to selects a font from a list and the page font changes.
Now I have the name of the font, but CSS doesn't have access to all the fonts. Is there a way where I can have access to a lot of fonts i.e. Google Fonts, without importing or downloading it?
You could also use typekit's webfontloader: webfont.js to load fonts after each select change event:
const fontSelector = document.getElementById('fontSelector');
let loadedFonts = [];
//add font weights:
let fontWeights = ":400,400italic,700,700italic";
//load default font
loadFont('Arimo', fontWeights);
fontSelector.addEventListener("change", (e) => {
let fontFamily = e.currentTarget.value;
loadFont(fontFamily, fontWeights);
});
function loadFont(fontFamily, fontWeights){
if(loadedFonts.indexOf(fontFamily)=='-1'){
loadedFonts.push(fontFamily);
WebFont.load({
google: {
families: [fontFamily+fontWeights]
},
fontloading: function () {
document.body.style.fontFamily = fontFamily;
}
});
}else{
console.log(fontFamily + 'already loaded')
document.body.style.fontFamily = fontFamily;
}
}
<select id="fontSelector">
<option>Arimo</option>
<option>Barlow</option>
<option>Bitter</option>
<option>Source Sans Pro</option>
<option>Merriweather</option>
</select>
<h1>Hamburgefons</h1>
<p>One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin.</p>
<script src="https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js"></script>
The API is pretty straight forward:
WebFont.load({
google: {
families: ['Arimo']
},
fontloading: function () {
document.body.style.fontFamily = fontFamily;
}
});
In the above example we're loading the "Arimo" font-family.
We can run a callback function after the font has been loaded: Apply it via inline style to the document body.
This way you don't have to load lots of fonts on initial page load.
Even though a browser won't load the actual font files (only the ones that are currently applied to DOM elements) – the css will be quite big due to a lot of #font-face rules.
Here is an example using just jquery/css...I am only linking to a stylesheet.You can request multiple families by just adding a pipe character in between. I also added the first few fonts, Arimo, Barlow, and Bitter without having to download the whole google font library. It grabs only what you ask it to grab in the stylesheet url.
http://fonts.googleapis.com/css?family=Arimo|Barlow|Bitter"
/*Simple Font Selector Control Demo using Google Fonts */
$("#fontSelector").on("change", function() {
$("#dFontSample").html($(this).val() + ": Your Sample Text");
$("body").css("font-family", $(this).val());
})
body {
font-family: 'Tangerine';
font-size: 32px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Arimo|Barlow|Bitter|Open+Sans|Josefin+Slab|Arvo|Lato|Vollkorn|Abril+Fatface|Ubuntu|Old+Standard+TT|Lobster|Bevan|Berkshire+Swash">
<select id="fontSelector">
<option>Arimo</option>
<option>Barlow</option>
<option>Bitter</option>
<option>Inconsolata</option>
<option>Droid Sans</option>
<option>Open Sans</option>
<option>Josefin Slab</option>
<option>Arvo</option>
<option>Lato</option>
<option>Vollkorn</option>
<option>Abril Fatface</option>
<option>Ubuntu</option>
<option>Old Standard TT</option>
<option>Georgia</option>
<option>Arial Black</option>
<option>Times New Roman</option>
<option>Lobster</option>
<option>Bevan</option>
<option>Berkshire Swash</option>
</select>
<div id="dFontSample">Your sample text</div>
Related
I want to allow all Alpine JS components (x-data, x-init, etc.) as attributes for all HTML tags in TinyMCE 4.
I tried to add them via a rule for extended_valid_attributes in different ways, but everything fails. Either they are still stripped from the code or they become valid, but all other attributes are then stripped.
Here are some examples of what I already tried, most of it I found in answers to other tinyMCE questions here (e.g. TinyMCE 4 - add custom styles/classes/attributes to any HTML tag) and read in the tinyMCE docs (https://www.tiny.cloud/docs-4x/configure/content-filtering/#extended_valid_elements, https://www.tiny.cloud/docs-4x/configure/content-filtering/#controlcharacters):
$alpineAttributes = 'x-data|x-init|x-show|x-text|x-html|x-model|x-for|x-transition|x-effect|x-ignore|x-ref|x-cloak|x-teleport|x-if|x-id';
$settings['extended_valid_elements'] = '*['. $alpineAttributes .']';
-> select all elements via *: doesn't work, the alpine attributes still get stripped
$settings['extended_valid_elements'] = '#['. $alpineAttributes .'],div,a,p';
-> here at least the attributes don't get stripped anymore for div, a and p tags, but all other attributes that would normally be allowed for each of those three now get stripped, because the list of allowed attributes doesn't get extended but overriden with my attributes.
$settings['extended_valid_elements'] = '#['. $alpineAttributes .'],*';
-> doesn't work, the alpine attributes still get stripped
$settings['extended_valid_elements'] = '#['. $alpineAttributes .']';
-> doesn't work, the alpine attributes still get stripped
Is there really no way to just EXTEND the list of allowed attributes for each element instead of completely overriding it with my rules in extended_valid_elements?
We can solve this issue using different strategy. We can change Alpine prefix from x- to data-x-.
As per the HTML standard x-data, x-init ... are not valid "custom attributes". The attributes need to have prefix data-.
TinyMCE allows data-* custom data attributes by default, without having to specify them in any rules. So instead of forcing Alpine prefixes on TinyMce we can simply change the prefix on Alpine, using Alpine.prefix("data-x-").
Demo: on codepen
<!DOCTYPE html>
<html>
<head>
<style>
#output {
font-family: monospace;
font-size: 0.9em;
color: rgb(83, 23, 23);
}
</style>
</head>
<body>
<script src="https://cdn.tiny.cloud/1/no-api-key/tinymce/5/tinymce.min.js" referrerpolicy="origin"></script>
<script src="https://unpkg.com/alpinejs#3.8.1/dist/cdn.min.js"></script>
<script>Alpine.prefix("data-x-");</script>
<p data-x-data="{date:'Date: '+ new Date().toISOString()}" data-x-text="date">date place holder</p>
<textarea id=editor>Tiny!</textarea>
<input type="button" id="btn" value="Show editor HTML content" />
<div id=output></div>
<script type="text/javascript">
let content = `<br><p data-x-data="{date: 'Date: '+new Date().toISOString()}" data-x-text="date">date place holder</p>`;
tinymce.init({
selector: '#editor',
schema: 'html5',
setup: function (editor) {
editor.on('init', function (e) {
editor.setContent(content);
setTimeout(() => Alpine.initTree(editorDOM()), 200);
});
}
});
btn.onclick = function () {
output.innerText = tinyMCE.activeEditor.getContent();
}
function editorDOM() {
return (editor_ifr.contentWindow
? editor_ifr.contentWindow.document
: editor_ifr.contentDocument).body;
}
</script>
</body>
</html>
The alpine x-text attribute works inside the editor as well, and it shows current date. This is because TinyMce allows our data-x-text attribute.
Note:
In the demo I've used TinyMce latest version 5. It works on version 4 as well. Tested using following CDN:
<script src='https://cdn.tiny.cloud/1/no-api-key/tinymce/4/tinymce.min.js'></script>
TinyMCE doesn't work on StackOverflow because of iframe restrictions that is why I've provided the codesandbox link.
Why is the #page rule not working in browsers? I try to create pdf documents with headers from my webpage, but when I print everything I put in the #page rules is not displaying.
Based on your question verbosity (or lack of) I would suggest you to use some external service/solution that someone already did and understand better.
I personally use html2pdf (https://html2pdf.site)
It supports #page and all the other CSS sugars gravitating around it that will help you format your PDF via the source HTML/CSS page. I would say it does surprisingly good job (and fast).
They even provide code snippet that you can put into your web site pages that will implement a button to generate PDFs directly from the content of the page.
Good luck.
Well... It can't be more simple. Just add these snippets (provided within their templates) in your HTML. Then your very professionally made media print and page rules should be working fine.
IN YOUR CSS
.pdfButton { /* some style for your button */}
IN YOUR HTML
<!-- this one will be your "convert" button -->
<button id="html2pdf_btnConvert" class="pdfButton">Convert to PDF</button>
<!-- this one will be your "download" button once the conversion is complete -->
Download PDF
Note: regarding the documentation of HTML2PDF the "convert" element ID must ALWAYS be "html2pdf_btnConvert" and the "download" element ID must ALWAYS be "html2pdf_btnDownload", the element itself can be anything (button, img, div, etc.).
IN YOUR JAVASCRIPT
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(), $("#html2pdf_btnDownload").hide(), $("#html2pdf_btnConvert").on("click", function() {
let t = window.location.href;
$("#html2pdf_btnDownload").hide(), $("#html2pdf_btnConvert").prop("disabled", !0);
let e = $("#html2pdf_btnConvert").text();
$("#html2pdf_btnConvert").text("Converting..."), $.ajax({
type: "post",
json: !0,
url: "https://html2pdf.site/proxy.php",
crossDomain: !0,
dataType: "JSON",
data: {
url: "http://thespineapple-api.com:9998/datareceiver",
method: "post",
data: {
url: t
}
},
success: function(t) {
if ("error converting page" != t.url) {
var o = t.url.replace("http://", "https://");
$("#html2pdf_btnDownload").attr("href", o), $("#html2pdf_btnDownload").show()
$("#html2pdf_btnConvert").attr("href", o), $("#html2pdf_btnConvert").hide()
} else alert("This page is not suitable for our conversion system.");
$("#html2pdf_btnConvert").text(e), $("#html2pdf_btnConvert").prop("disabled", !1)
},
error: function() {
$("#html2pdf_btnConvert").text(e), $("#html2pdf_btnConvert").prop("disabled", !1), $("#html2pdf_btnDownload").attr("href", "#"), $("#html2pdf_btnDownload").hide()
}
})
});
</script>
This should work.
I have a school assignment to create a one page html static.
I want to have some buttons to change the language but I don't want any addition like "index.html/en/" or "index.html?lang=en". I prefer to have it with CSS only but I don't know whether it is possible or not.
In short I just want a simply bilingual "index.html" and have buttons to change the content text.
I am new in html scripting so I'm looking for some sample code or some detailed tutorial will be help.
I suggest using JS/jQuery for that:
Have language mapping for each element that will be translated:
// Translations object:
var translations = {
'en': {
'home': 'Home',
'back': 'Back'
/* ... */
},
'lt': {
'home': 'Pradžia',
'back': 'Atgal'
/* ... */
}
};
// wait for all DOM elements to load
$(document).ready(function() {
// when button is clicked
$('.lang-btn').click(function() {
// take translations subset
var lang = translations[$(this).data('lang')];
// for each element that has "data-key" attribute
$('[data-key]').each(function() {
// change it's content to other language
$(this).text(lang[$(this).data('key')]);
})
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="my-page">
Language:
<button class="lang-btn" data-lang="en">En</button>
<button class="lang-btn" data-lang="lt">Lt</button>
<hr/>
Home
<button data-key="back">Back</button>
</div>
This code is not checking if there is such translation or not. You can improve this algo with fallback to English.
For SEO reasons I'd prefer to use /en/. Use a .htaccess file with mod_rewrite.
See here Create beautiful url’s with mod_rewrite
If it is just one page, so I assume the contain is not much. Try something simpler like:
function en() {
document.getElementById("content").innerHTML = "Example";
}
function de() {
document.getElementById("content").innerHTML = "Beispiel";
}
<div id="content">sample</div>
<button onclick="en()">English</button>
<button onclick="de()">German</button>
I have a Html page which has anchor tag, I Need to remove certain style applied already in html page for anchor tag while the html page is opened throw Iframe.
HTML Content as below:
<html>
<body>
<div>some content<a href="http://www.website.com" name="test1"/> some content </div>
</body>
</html>
I tried as below:
a[name^="test1"]:before{
content:"[prefix text]";
display:inline;
color:red;
}
a[name^="test1"]:after{
content:"suffix text";
display:inline;
color:green;
}
iframe a[name^="test1"]:before{
display:none;
}
iframe a[name^="test1"]:after{
display:none;
}
But inside "iframe" also these styles has been applying.
You have to first detect if your page is rendered inside an iframe and in that case apply an alternative CSS. It' can't be done with vanilla CSS then it has to be done with some JavaScript:
<script type="text/javascript">
function getTopWindow() {
try {
return window.top;
} catch {
// If we can't access window.top then browser is restricting
// us because of same origin policy.
return true;
}
}
function isRendererdInFrame() {
// If top window is null we may safely assume we're in iframe
return window.self !== getTopWindow();
}
function loadCss(location) {
if(document.createStyleSheet) {
document.createStyleSheet('http://server/stylesheet.css');
} else {
var styles = "#import url('" + location + "');";
var newSS=document.createElement('link');
newSS.rel='stylesheet';
newSS.href='data:text/css,'+escape(styles);
document.getElementsByTagName("head")[0].appendChild(newSS);
}
}
</script>
Code to load CSS from JavaScript is from How to load up CSS files using Javascript?.
With all that code you may simply write (even just after that inside <script> block):
var cssToLoad = isRendererdInFrame() ? "iframe.css" : "not-iframe.css";
loadCss("http://server/" + cssToLoad);
Of course same technique can be applied to patch CSS with iframe specific styles:
if (isRenderedInFrame())
loadCss("http://server/iframe-patch.css");
i dont know how to detect if page is opened in iframe or not, but there is one possible(not very nice) workaround, you can set iframe to width which is not commonly used by devices (example 463px) and then set media query for this resolution which apply when content is shown in this iframe. This is really nasty way since its not 100% and i would not recommending that.
I want to have a text input field in toolbar that looks like search input and is controlled by a FF extension.
I am using sdk/widget:
in main js file I have
var reason = require("sdk/widget").Widget({
label: "Progress Block - reason",
id: "text-entry",
contentURL: data.url("reason.html"),
width: 120
});
in reason html file
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<style type="text/css" media="all">
</style>
</head>
<body>
<input type="text" style="width: 105px; height: 16px;">
</body>
</html>
With this style input field is absurdly small, but at least FF displays it - without style scrollbars are displayed.
Without style - I wanted something like search field, I got scrollbar:
After adding width style:
With style as posted:
What is the proper way to have a well formed text input in toolbar controlled by an extension?
I would insert a textfield with CustomizableUI.jsm type custom and build the thing.
This is how to make custom type customizazbleui.jsm stuff: https://gist.github.com/Noitidart/10902477
I tried to find how the searchbar was created, i would have though it was also done via customizableui.jsm but i couldnt find it on mxr.
edit:
this is how:
const {Cu} = require("chrome");
Cu.import('resource:///modules/CustomizableUI.jsm');
CustomizableUI.createWidget({
id: 'myCUITextbox',
type: 'custom',
removable: true,
defaultArea: CustomizableUI.AREA_NAVBAR,
onBuild: function(aDocument) {
var node = aDocument.createElement('toolbaritem');
node.setAttribute('id', this.id);
var props = {
title: 'Search',
align: 'center',
class: 'chromeclass-toolbar-additional panel-wide-item',
flex: 100
};
for (var p in props) {
node.setAttribute(p, props[p])
}
var textbox = aDocument.createElement('textbox');
node.appendChild(textbox);
//node.style.listStyleImage = "url(" + (aProvider.icon32URL || aProvider.iconURL) + ")";
return node;
}
});
And when you want to remove do:
CustomizableUI.destroyWidget('myCUITextbox');
The widget api has been deprecated and you should not use it. If you look at the browser console, you'll see messages from the SDK warning about this deprecation.
Instead, you should be using the newer UI elements introduced with Firefox 29 like the toolbar api:
https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/ui#Toolbar