├── .github └── FUNDING.yml ├── .prettierrc.json ├── CHANGELOG.md ├── LICENSE ├── README.md ├── TODO.md ├── assets ├── logo-wide.psd ├── logo.psd ├── promo1.jpg ├── promo2.jpg └── promo3.jpg └── src ├── background.js ├── content.js ├── images └── logo │ ├── V1 │ ├── icon128.png │ ├── icon16.png │ ├── icon48.png │ └── logo-wide2.png │ └── V2 │ ├── icon128.png │ ├── icon16.png │ ├── icon48.png │ ├── logo-wide.png │ └── logo_original.png ├── manifest.json ├── popup.css ├── popup.html └── popup.js /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | ko_fi: amranich 4 | -------------------------------------------------------------------------------- /.prettierrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "tabWidth": 4, 3 | "singleQuote": true, 4 | "trailingComma": "es5", 5 | "printWidth": 150, 6 | "endOfLine": "auto", 7 | "semi": true 8 | } 9 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | ## v3.0 (2024-02-09) 4 | 5 | - Changed extension logo base color. 6 | - Changed extension UI look and feel. 7 | - Supported changing fonts size [#17](https://github.com/AmraniCh/github-code-font-changer/pull/17). 8 | 9 | ## v2.2 (2023-02-28) 10 | 11 | - Supported changing fonts with the new feature [New Code Search and Code View](https://docs.github.com/en/repositories/working-with-files/managing-files/navigating-files-with-the-new-code-view) - [#16](https://github.com/AmraniCh/github-code-font-changer/pull/16). 12 | 13 | ## v2.0 (2022-04-10) 14 | 15 | - Allows using local fonts beside the fonts provided by the extension. 16 | - Applying the choosing font family and weight is now live as you type in the fields. 17 | - Added new font `Atma` [#14](https://github.com/AmraniCh/github-code-font-changer/pull/14). 18 | 19 | ## v1.3 (2021-10-05) 20 | 21 | - `Space Mono`, `Inconsolata`, `Anonymous Pro`, `IBM Plex Mono` fonts added [#5](https://github.com/AmraniCh/github-code-font-changer/pull/5). 22 | - The fonts dropdown items are now sorted alphabetically. 23 | 24 | ## v1.2 (2021-9-26) 25 | 26 | - `Share Tech Mono`, `PT Mono`, `Oxygen Mono` fonts added [#4](https://github.com/AmraniCh/github-code-font-changer/pull/4). 27 | - Fixed dropdown bug with fonts that haven't any regular/italic styles [#4](https://github.com/AmraniCh/github-code-font-changer/pull/4). 28 | 29 | ## v1.1 (2021-9-12) 30 | 31 | - `Roboto Mono`, `Ubuntu Mono`, `Courier Prime`, `JetBrains Mono` fonts added [#2](https://github.com/AmraniCh/github-code-font-changer/pull/2). 32 | 33 | ## v1.0 (2021-7-1) 34 | 35 | - First Stable Release. -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 AmraniCh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 | 3 |

4 | GitHub Code Font Changer 5 |

6 | 7 |

8 |
9 | 10 | version 12 | 13 | 14 | repo code size 16 | 17 | 18 | license 20 | 21 | 22 | lang 24 | 25 |

26 | 27 |

28 | Developped and maintained with ❤️ by AmraniCh and contributors. 29 |

30 | 31 | # 32 | 33 | Unhappy with the boring GitHub code viewer font? You're looking for a way to change it without affecting other website's fonts? Well, this extension is made for you :raised_hands:. 34 | 35 | ## 🚀 Features 36 | 37 | - As of Version 3, changing font size is supported. 38 | - As of version 2, you can use the fonts installed in your local machine alongside the fonts provided by the extension. 39 | - Change the font weight for the selected font family. 40 | - Hide the indentation guides. 41 | 42 | ## 🔥 Available Fonts 43 | 44 | - Anonymous Mono. 45 | - Atma. 46 | - Courier Prime. 47 | - Fira Code. 48 | - Inconsolata. 49 | - IBM Plex Mono. 50 | - JetBrains Mono. 51 | - Oxygen Mono. 52 | - PT Mono. 53 | - Roboto Mono. 54 | - Share Tech Mono. 55 | - Source Code Pro. 56 | - Space Mono. 57 | - Ubuntu Mono. 58 | 59 | If you have some other font that other developers could like, you can send a pull request to add it to this extension (The font must be free for personal use or licensed under a free open source license). 60 | 61 | ## 🌠 Download 62 | 63 | You can download this extension via : 64 | 65 | ### Via Chrome Store 66 | 67 | 68 | 69 | ### Manual Installation Using Developer Mode 70 | 71 | You can also manually install it via the developer mode in your browser. 72 | 73 | 1. Download the last stable version from [releases tab](https://github.com/AmraniCh/github-code-font-changer/releases). 74 | 2. Unzip the downloaded zip file. 75 | 3. Visit `chrome://extensions`. 76 | 4. Enable the developer mode. 77 | 5. Click `Load unpacked` button. 78 | 6. Select the `src` folder from the unzipped file. 79 | 7. Have fun :blush:! 80 | 81 | ## 🗃️ Assets 82 | 83 | Logo assets (PSD files) are available in the assets folder, If you see a need for changing something in the logo or you have a creative idea feel free to download and edit the PSD file. 84 | 85 | For the store chrome Promo images they are created with Figma, with this If you want to edit them, you have first to open an issue describing your idea and what you will change, if approved I will invite you the [source Figme file](https://www.figma.com/file/7SHQlL5ehiQhxWpz4DkQpO/github-code-font-changer?type=design&node-id=0-1&mode=design&t=qv0LXPwBsEHsJ55M-0). 86 | 87 | ## ⭐ Rate The Extension If You Found It Useful 88 | 89 | If you find this extension useful for you please give us a review at [Chrome Web Store](https://chrome.google.com/webstore/detail/github-code-font-changer/jmbophfanmlfkloliildahlpnlbojhfi), this will motivate us to improve and maintain this extension in the future, Thank you :) 90 | 91 | ## 📜 License 92 | 93 | GitHub Code Font Changer is an open-source chrome extension lisenced under the [MIT](https://github.com/AmraniCh/github-code-font-changer/blob/master/LICENSE) open source license. 94 | -------------------------------------------------------------------------------- /TODO.md: -------------------------------------------------------------------------------- 1 | # TODO LIST 2 | 3 | Here you can find/add some of the desired features to be implemented in the next versions of this extension. 4 | 5 | ## Requested features 6 | 7 | - [X] Add the option to change the font size of the selected font family [#8](../../issues/8). 8 | - [x] Allows using local fonts [#12](../../issues/12). 9 | -------------------------------------------------------------------------------- /assets/logo-wide.psd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmraniCh/github-code-font-changer/b836bb65a65da67ced9e94a030d1ea04447f440a/assets/logo-wide.psd -------------------------------------------------------------------------------- /assets/logo.psd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmraniCh/github-code-font-changer/b836bb65a65da67ced9e94a030d1ea04447f440a/assets/logo.psd -------------------------------------------------------------------------------- /assets/promo1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmraniCh/github-code-font-changer/b836bb65a65da67ced9e94a030d1ea04447f440a/assets/promo1.jpg -------------------------------------------------------------------------------- /assets/promo2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmraniCh/github-code-font-changer/b836bb65a65da67ced9e94a030d1ea04447f440a/assets/promo2.jpg -------------------------------------------------------------------------------- /assets/promo3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmraniCh/github-code-font-changer/b836bb65a65da67ced9e94a030d1ea04447f440a/assets/promo3.jpg -------------------------------------------------------------------------------- /src/background.js: -------------------------------------------------------------------------------- 1 | var /** 2 | * Supported fonts 3 | */ 4 | fonts = { 5 | 'Fira Code': 'https://fonts.googleapis.com/css2?family=Fira+Code:wght@300;400;500;600;700&display=swap', 6 | 'Source Code Pro': 'https://fonts.googleapis.com/css2?family=Source+Code+Pro:wght@200;300;400;500;600;700;900&display=swap', 7 | 'Roboto Mono': 'https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@100;200;300;400;500;600;700&display=swap', 8 | 'Ubuntu Mono': 'https://fonts.googleapis.com/css2?family=Ubuntu+Mono:wght@400;700&display=swap', 9 | 'Courier Prime': 'https://fonts.googleapis.com/css2?family=Courier+Prime:wght@400;700&display=swap', 10 | 'JetBrains Mono': 'https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@100;200;300;400;500;600;700;800&display=swap', 11 | 'Share Tech Mono': 'https://fonts.googleapis.com/css2?family=Share+Tech+Mono&display=swap', 12 | 'Fira Mono': 'https://fonts.googleapis.com/css2?family=Fira+Mono:wght@400;500;700&display=swap', 13 | 'PT Mono': 'https://fonts.googleapis.com/css2?family=PT+Mono&display=swap', 14 | 'Oxygen Mono': 'https://fonts.googleapis.com/css2?family=Oxygen+Mono&display=swap', 15 | 'Space Mono': 'https://fonts.googleapis.com/css2?family=Space+Mono:ital,wght@0,400;0,700;1,400;1,700&display=swap', 16 | Inconsolata: 'https://fonts.googleapis.com/css2?family=Inconsolata:wght@200;300;400;500;600;700;800;900&display=swap', 17 | 'Anonymous Pro': 'https://fonts.googleapis.com/css2?family=Anonymous+Pro:ital,wght@0,400;0,700;1,400;1,700&display=swap', 18 | 'IBM Plex Mono': 19 | 'https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;1,100;1,200;1,300;1,400;1,500;1,600;1,700&display=swap', 20 | Atma: 'https://fonts.googleapis.com/css2?family=Atma:wght@300;400;500;600;700&display=swap', 21 | }, 22 | selectors = { 23 | code: '.blob-code-inner, .react-code-text, .react-blob-print-hide', 24 | intentGuides: '[data-rgh-whitespace="space"]', 25 | }; 26 | 27 | // add a listener to tabs.onUpdated event 28 | chrome.tabs.onUpdated.addListener(function (tabId, info) { 29 | // if the tab is completely loaded 30 | if (info.status === 'complete') { 31 | chrome.storage.sync.get(['gt_font_family', 'gt_font_weight', 'gt_font_size', 'gt_font_link', 'gt_indent_guides'], function (data) { 32 | if (Object.keys(data).length > 0) { 33 | chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { 34 | chrome.tabs.sendMessage(tabs[0].id, { 35 | type: 'loadFont', 36 | font: { 37 | font: data.gt_font_family, 38 | link: data.gt_font_link, 39 | }, 40 | }); 41 | }); 42 | 43 | applyFontFamily(data.gt_font_family); 44 | applyFontWeight(data.gt_font_weight); 45 | applyFontSize(data.gt_font_size); 46 | 47 | data.gt_indent_guides ? showIndentGuides() : hideIndentGuides(); 48 | } 49 | }); 50 | 51 | // Intercept the load font message from the popup script 52 | // and resend the same request to the content script 53 | chrome.runtime.onMessage.addListener(function (request) { 54 | if (request.type === 'loadFont') { 55 | chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { 56 | chrome.tabs.sendMessage(tabs[0].id, request); 57 | }); 58 | } 59 | }); 60 | } 61 | }); 62 | 63 | /** 64 | * Apply the giving css styles to Github code container 65 | * @param {String} styles 66 | * @returns true 67 | */ 68 | function applyStyles(selector, styles) { 69 | const css = stylesToCss(styles); 70 | chrome.tabs.insertCSS({ 71 | code: `${selector} {${css}}`, 72 | }); 73 | } 74 | 75 | /** 76 | * Converts the giving css styles object into a regular CSS string 77 | * @param {Object} styles 78 | * @returns {String} 79 | */ 80 | function stylesToCss(styles) { 81 | var css = ''; 82 | for (let property in styles) { 83 | const value = styles[property]; 84 | if (!Object.prototype.hasOwnProperty.call(styles, property) || !value) { 85 | continue; 86 | } 87 | css += `${property}: ${value} !important;`; 88 | } 89 | 90 | return css; 91 | } 92 | 93 | /** 94 | * Applies the giving font family to the html github code container 95 | * @param {String} family 96 | */ 97 | function applyFontFamily(family) { 98 | applyStyles(selectors.code, { 'font-family': family }); 99 | chrome.extension.sendMessage({ 100 | type: 'loadFont', 101 | font: { 102 | font: family, 103 | link: fonts[family], 104 | }, 105 | }); 106 | } 107 | 108 | /** 109 | * Applies the provided weight to the html github code container 110 | * @param {String} weight 111 | */ 112 | function applyFontWeight(weight) { 113 | applyStyles(selectors.code, { 'font-weight': weight }); 114 | } 115 | 116 | /** 117 | * Applies the provided font-size to the html github code container 118 | * @param {String} font-size (in px, em, rem, etc) 119 | */ 120 | function applyFontSize(size) { 121 | applyStyles(selectors.code, { 'font-size': size }); 122 | } 123 | 124 | function hideIndentGuides() { 125 | applyStyles(selectors.intentGuides, { visibility: 'hidden' }); 126 | } 127 | 128 | function showIndentGuides() { 129 | applyStyles(selectors.intentGuides, { visibility: 'visible' }); 130 | } 131 | 132 | /** 133 | * Just a shortcut for the native target.addEventListener 134 | * @param {DOMElement} ele 135 | * @param {String} event 136 | * @param {function} handler 137 | */ 138 | function addEvent(ele, event, handler) { 139 | ele.addEventListener(event, handler.bind(this), false); 140 | } 141 | -------------------------------------------------------------------------------- /src/content.js: -------------------------------------------------------------------------------- 1 | chrome.runtime.onMessage.addListener((message) => { 2 | if (message.type === 'loadFont') { 3 | const style = document.createElement('style'); 4 | style.appendChild(document.createTextNode(`@import url(${message.font.link})`)); 5 | document.head.appendChild(style); 6 | } 7 | return true; 8 | }); 9 | -------------------------------------------------------------------------------- /src/images/logo/V1/icon128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmraniCh/github-code-font-changer/b836bb65a65da67ced9e94a030d1ea04447f440a/src/images/logo/V1/icon128.png -------------------------------------------------------------------------------- /src/images/logo/V1/icon16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmraniCh/github-code-font-changer/b836bb65a65da67ced9e94a030d1ea04447f440a/src/images/logo/V1/icon16.png -------------------------------------------------------------------------------- /src/images/logo/V1/icon48.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmraniCh/github-code-font-changer/b836bb65a65da67ced9e94a030d1ea04447f440a/src/images/logo/V1/icon48.png -------------------------------------------------------------------------------- /src/images/logo/V1/logo-wide2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmraniCh/github-code-font-changer/b836bb65a65da67ced9e94a030d1ea04447f440a/src/images/logo/V1/logo-wide2.png -------------------------------------------------------------------------------- /src/images/logo/V2/icon128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmraniCh/github-code-font-changer/b836bb65a65da67ced9e94a030d1ea04447f440a/src/images/logo/V2/icon128.png -------------------------------------------------------------------------------- /src/images/logo/V2/icon16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmraniCh/github-code-font-changer/b836bb65a65da67ced9e94a030d1ea04447f440a/src/images/logo/V2/icon16.png -------------------------------------------------------------------------------- /src/images/logo/V2/icon48.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmraniCh/github-code-font-changer/b836bb65a65da67ced9e94a030d1ea04447f440a/src/images/logo/V2/icon48.png -------------------------------------------------------------------------------- /src/images/logo/V2/logo-wide.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmraniCh/github-code-font-changer/b836bb65a65da67ced9e94a030d1ea04447f440a/src/images/logo/V2/logo-wide.png -------------------------------------------------------------------------------- /src/images/logo/V2/logo_original.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AmraniCh/github-code-font-changer/b836bb65a65da67ced9e94a030d1ea04447f440a/src/images/logo/V2/logo_original.png -------------------------------------------------------------------------------- /src/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "manifest_version": 2, 3 | "name": "Github Code Font Changer", 4 | "description": "Change and customize the boring GitHub code viewer font", 5 | "version": "3.0", 6 | "icons": { 7 | "16": "images/logo/V2/icon16.png", 8 | "48": "images/logo/V2/icon48.png", 9 | "128": "images/logo/V2/icon128.png" 10 | }, 11 | "browser_action": { 12 | "default_popup": "popup.html" 13 | }, 14 | "background": { 15 | "scripts": [ 16 | "background.js" 17 | ] 18 | }, 19 | "content_scripts": [ 20 | { 21 | "matches": [ 22 | "https://*.github.com/*" 23 | ], 24 | "run_at": "document_idle", 25 | "js": [ 26 | "content.js" 27 | ] 28 | } 29 | ], 30 | "permissions": [ 31 | "storage", 32 | "https://*.github.com/*" 33 | ] 34 | } -------------------------------------------------------------------------------- /src/popup.css: -------------------------------------------------------------------------------- 1 | :root { 2 | --primary-color: #298f8e 3 | } 4 | 5 | * { 6 | margin: 0; 7 | padding: 0; 8 | box-sizing: border-box; 9 | color: var(--primary-color); 10 | font-weight: 500; 11 | } 12 | 13 | input[type="checkbox"] { 14 | accent-color: var(--primary-color); 15 | } 16 | 17 | input::placeholder { 18 | color: var(--primary-color); 19 | opacity: .6; 20 | } 21 | 22 | .form { 23 | width: 320px; 24 | background-color: #fff; 25 | border-right: 6px solid var(--primary-color); 26 | border-bottom: 6px solid var(--primary-color); 27 | padding: 15px 10px 15px 10px; 28 | } 29 | 30 | .title { 31 | text-align: left; 32 | background-color: #fff; 33 | text-transform: uppercase; 34 | margin-bottom: 15px; 35 | padding: 15px 15px; 36 | display: flex; 37 | align-items: center; 38 | border-bottom: 2px solid var(--primary-color); 39 | } 40 | 41 | .title img.logo { 42 | width: 100%; 43 | } 44 | 45 | .title span { 46 | font-size: 24px; 47 | margin-left: 10px; 48 | } 49 | 50 | .label { 51 | font-size: .9rem; 52 | } 53 | 54 | .input-field, 55 | .checkbox-field { 56 | margin-bottom: 10px; 57 | padding: 0 8px; 58 | } 59 | 60 | .input-field .label::first-letter { 61 | text-transform: uppercase; 62 | } 63 | 64 | .input-field .label { 65 | display: block; 66 | margin-bottom: 8px; 67 | text-align: left; 68 | } 69 | 70 | .input-field .control { 71 | transition: all .1s linear; 72 | padding: 8px; 73 | width: 100%; 74 | font-size: 1.2rem; 75 | margin-top: 8px; 76 | border: 2px solid var(--primary-color); 77 | border-radius: 5px; 78 | } 79 | 80 | .input-field select.control { 81 | border: 1px solid #ddd; 82 | text-align: center; 83 | } 84 | 85 | .input-field .control:focus { 86 | outline: 3px solid var(--primary-color); 87 | } 88 | 89 | .checkbox-field .control { 90 | margin-right: 5px; 91 | width: 15px; 92 | height: 15px; 93 | } 94 | 95 | .checkbox-field .label, 96 | .checkbox-field .control { 97 | vertical-align: middle; 98 | } 99 | 100 | .apply-btn { 101 | width: 100%; 102 | background-color: rgb(9 132 227); 103 | padding: 12px 20px; 104 | color: rgb(255 255 255); 105 | border-radius: 2px; 106 | border: none; 107 | cursor: pointer; 108 | margin-top: 35px; 109 | text-transform: uppercase; 110 | transition: all 0.3s ease-in-out; 111 | } 112 | 113 | .apply-btn:hover { 114 | opacity: 0.8; 115 | } -------------------------------------------------------------------------------- /src/popup.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Popup 5 | 6 | 7 | 8 | 9 |
10 |

11 | 12 |

13 | 14 |
15 | 20 |
21 | 22 |
23 | 28 |
29 | 30 |
31 | 35 |
36 | 37 |
38 | 39 | 40 |
41 |
42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /src/popup.js: -------------------------------------------------------------------------------- 1 | const /** 2 | * variables from background 3 | */ 4 | background = chrome.extension.getBackgroundPage(), 5 | addEvent = background.addEvent, 6 | applyStyles = background.applyStyles, 7 | selectors = background.selectors, 8 | applyFontFamily = background.applyFontFamily, 9 | applyFontWeight = background.applyFontWeight, 10 | applyFontSize = background.applyFontSize, 11 | showIndentGuides = background.showIndentGuides, 12 | hideIndentGuides = background.hideIndentGuides, 13 | fonts = background.fonts, 14 | /** 15 | * Popup DOM elements 16 | */ 17 | fontsDatalist = document.querySelector('#font_family_list'), 18 | fontsDatalistInput = document.querySelector('#font_family'), 19 | weightsDatalist = document.querySelector('#font_weight_list'), 20 | weightsDatalistInput = document.querySelector('#font_weight'), 21 | fontSizeInput = document.querySelector('#font_size'), 22 | IndentGuidesCheckbox = document.querySelector('#indentGuides'); 23 | 24 | // popup document content loaded 25 | addEvent(document, 'DOMContentLoaded', function () { 26 | initEvents(); 27 | fillFontsDrodown(); 28 | updateUIFromStorage(); 29 | }); 30 | 31 | /** 32 | * Binding the necessary events to popup DOM elements 33 | */ 34 | function initEvents() { 35 | addEvent(fontsDatalistInput, 'input', function () { 36 | const oldSelectedWeight = weightsDatalistInput.value, 37 | fontSelected = fontsDatalistInput.value, 38 | isLocalFont = Object.keys(fonts).indexOf(fontSelected) === -1; 39 | 40 | applyFontFamily(fontSelected); 41 | 42 | chrome.storage.sync.set({ 43 | gt_font_family: fontSelected, 44 | gt_font_link: fonts[fontSelected], 45 | }); 46 | 47 | if (!isLocalFont) { 48 | fillWeightsDropdown(fontsDatalistInput.value); 49 | updateSelectedWeight(oldSelectedWeight); 50 | } 51 | }); 52 | 53 | addEvent(weightsDatalistInput, 'input', function () { 54 | var selectedWeight = weightsDatalistInput.value; 55 | applyFontWeight(selectedWeight); 56 | chrome.storage.sync.set({ gt_font_weight: selectedWeight }); 57 | }); 58 | 59 | addEvent(fontSizeInput, 'input', function () { 60 | var typedSize = fontSizeInput.value; 61 | applyFontSize(typedSize); 62 | chrome.storage.sync.set({ gt_font_size: typedSize }); 63 | }); 64 | 65 | addEvent(IndentGuidesCheckbox, 'change', function (event) { 66 | var checked = event.target.checked; 67 | checked ? hideIndentGuides() : showIndentGuides(); 68 | chrome.storage.sync.set({ gt_indent_guide: !checked }); 69 | }); 70 | } 71 | 72 | /** 73 | * Populate options of the select font families dropdown 74 | */ 75 | function fillFontsDrodown() { 76 | const sortedFonts = sortObject(fonts); 77 | for (var fontName in sortedFonts) { 78 | if (!Object.prototype.hasOwnProperty.call(fonts, fontName)) { 79 | continue; 80 | } 81 | 82 | createOption(fontName, fontName, fontsDatalist); 83 | } 84 | } 85 | 86 | /** 87 | * Get font settings from storage and initialize the select dropdowns 88 | */ 89 | function updateUIFromStorage() { 90 | chrome.storage.sync.get(['gt_font_family', 'gt_font_weight', 'gt_font_size', 'gt_font_size', 'gt_indent_guide'], function (data) { 91 | console.log(data); 92 | if (Object.keys(data).length > 0) { 93 | const isLocalFont = Object.keys(fonts).indexOf(data.gt_font_family) === -1; 94 | 95 | // update fields 96 | fontsDatalistInput.value = data.gt_font_family || ''; 97 | weightsDatalistInput.value = data.gt_font_weight || ''; 98 | fontSizeInput.value = data.gt_font_size || ''; 99 | 100 | // update indentation guides checkbox 101 | IndentGuidesCheckbox.checked = !data.gt_indent_guide; 102 | 103 | if (!isLocalFont) { 104 | // fill the weights dropdown 105 | fillWeightsDropdown(fontsDatalistInput.value); 106 | } 107 | } 108 | }); 109 | } 110 | 111 | /** 112 | * fill weights dropdown options when selecting one of fonts in the font family dropdown 113 | * @param {String} family - selected font family 114 | */ 115 | function fillWeightsDropdown(family) { 116 | const link = fonts[family], 117 | weights = link.match(/\d{3}/g), 118 | weightsNames = { 119 | 100: 'thin', 120 | 200: 'extra light', 121 | 300: 'light', 122 | 400: 'regular', 123 | 500: 'medium', 124 | 600: 'semi-bold', 125 | 700: 'bold', 126 | 800: 'extra bold', 127 | 900: 'black', 128 | }; 129 | 130 | weightsDatalist.innerHTML = ''; 131 | 132 | var i = 0; 133 | try { 134 | while (i < weights.length) { 135 | var weight = weights[i]; 136 | createOption(`${weight} - ${weightsNames[weight]}`, weight, weightsDatalist); 137 | i++; 138 | } 139 | } catch (error) { 140 | // fonts which return null on weights.length 141 | var weight = 400; 142 | createOption(`${weight} - ${weightsNames[weight]}`, weight, weightsDatalist); 143 | } 144 | } 145 | 146 | /** 147 | * Updates font weights dropdown selected option 148 | * @param {String} oldSelectedWeight 149 | */ 150 | function updateSelectedWeight(oldSelectedWeight) { 151 | var option = weightsDatalist.querySelector(`option[value="${oldSelectedWeight}"]`); 152 | if (option === null) { 153 | /** 154 | * The first option was selected and the old font weight isn't supported by 155 | * the new selected font family, so we trigger the weights dropdown change 156 | * event to apply the first selected font weight. 157 | */ 158 | weightsDatalist.dispatchEvent(new Event('change')); 159 | } else { 160 | option.setAttribute('selected', ''); 161 | } 162 | } 163 | 164 | /** 165 | * Create option element for select dropdown 166 | */ 167 | function createOption(textContent, value, append) { 168 | var option = document.createElement('option'); 169 | 170 | option.textContent = textContent; 171 | option.value = value; 172 | 173 | append.appendChild(option); 174 | } 175 | 176 | function sortObject(obj) { 177 | return Object.keys(obj) 178 | .sort() 179 | .reduce((accumulator, current) => { 180 | accumulator[current] = obj[current]; 181 | return accumulator; 182 | }, {}); 183 | } 184 | --------------------------------------------------------------------------------