├── LICENSE ├── README.md ├── chrome ├── _locales │ └── en │ │ └── messages.json ├── images │ ├── wp-128.png │ ├── wp-16.png │ ├── wp-32.png │ ├── wp-48.png │ └── wp-logo.png ├── manifest.json └── src │ ├── background.js │ └── content-script.js └── firefox ├── _locales └── en │ └── messages.json ├── images ├── wp-128.png ├── wp-16.png ├── wp-32.png ├── wp-48.png └── wp-logo.png ├── manifest.json └── src ├── background.js └── content-script.js /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WordPressers on GitHub 2 | 3 | Show WordPress.org profiles for users on GitHub. Makes it very easy to find WordPress contributors on WordPress.org if they use different usernames on the two platforms. 4 | 5 | Everywhere you see their username on GitHub, you'll see a small WordPress logo with a link to their WordPress.org profile next to it. Hover over the logo to quickly see their username in a tooltip. Similarly, visit someone's GitHub profile and a link to their WordPress.org profile will appear in their bio. 6 | 7 | ## Download 8 | 9 | * [Download for 🦊 Firefox](https://addons.mozilla.org/en-US/firefox/addon/wordpressers-on-github/) 10 | * [Download for Google Chrome](https://chromewebstore.google.com/detail/wordpressers-on-github/hgomfjikakokcbkjlfgodhklifiplmpg) 11 | * (Safari version coming soon) 12 | 13 | ## Examples 14 | 15 | Single Commit 16 | 17 | Pull Request 18 | 19 | Profile 20 | 21 | ## Usage (Development Version) 22 | 23 | 1. Visit `chrome://extensions` 24 | 2. Select `Developer Mode` in the upper right corner. 25 | 3. Click on `Load unpacked` 26 | 4. Select the `wordpressers-on-github` directory 27 | 5. Click `Open` 28 | 6. Visit a GitHub repository such as https://github.com/WordPress/wordpress-develop/ and browse through pull requests. 29 | -------------------------------------------------------------------------------- /chrome/_locales/en/messages.json: -------------------------------------------------------------------------------- 1 | { 2 | "appTitle": { 3 | "message": "WordPressers on GitHub" 4 | }, 5 | "appDescription": { 6 | "message": "Shows WordPress.org profiles for users on GitHub." 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /chrome/images/wp-128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swissspidy/wordpressers-on-github/23bb4ce61f357ea33a831857d337bc83529e086c/chrome/images/wp-128.png -------------------------------------------------------------------------------- /chrome/images/wp-16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swissspidy/wordpressers-on-github/23bb4ce61f357ea33a831857d337bc83529e086c/chrome/images/wp-16.png -------------------------------------------------------------------------------- /chrome/images/wp-32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swissspidy/wordpressers-on-github/23bb4ce61f357ea33a831857d337bc83529e086c/chrome/images/wp-32.png -------------------------------------------------------------------------------- /chrome/images/wp-48.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swissspidy/wordpressers-on-github/23bb4ce61f357ea33a831857d337bc83529e086c/chrome/images/wp-48.png -------------------------------------------------------------------------------- /chrome/images/wp-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swissspidy/wordpressers-on-github/23bb4ce61f357ea33a831857d337bc83529e086c/chrome/images/wp-logo.png -------------------------------------------------------------------------------- /chrome/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "manifest_version": 3, 3 | "name": "WordPressers on GitHub", 4 | "version": "1.0.0", 5 | "description": "Show WordPress.org profiles for users on GitHub.", 6 | "default_locale": "en", 7 | "permissions": [ "storage" ], 8 | "host_permissions": [ 9 | "https://github.com/*", 10 | "https://profiles.wordpress.org/*" 11 | ], 12 | "background": { 13 | "service_worker": "src/background.js" 14 | }, 15 | "content_scripts": [ 16 | { 17 | "matches": [ "https://github.com/*" ], 18 | "js": [ "src/content-script.js" ] 19 | } 20 | ], 21 | "web_accessible_resources": [ 22 | { 23 | "resources": [ "images/wp-logo.png" ], 24 | "matches": [ "https://github.com/*" ] 25 | } 26 | ], 27 | "icons": { 28 | "16": "/images/wp-16.png", 29 | "32": "/images/wp-32.png", 30 | "48": "/images/wp-48.png", 31 | "128": "/images/wp-128.png" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /chrome/src/background.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Returns user information from cache. 3 | * 4 | * @param {string} githubUsername 5 | * @return {Promise} 6 | */ 7 | async function checkCache( githubUsername ) { 8 | const cache = await self.caches.open( 'wordpressers-on-github' ); 9 | const req = new Request( 10 | `https://profiles.wordpress.org/wp-json/wporg-github/v1/lookup/${ githubUsername }` 11 | ); 12 | const response = await cache.match( req ); 13 | if ( response ) { 14 | return response.json(); 15 | } 16 | return null; 17 | } 18 | 19 | /** 20 | * Store data in cache. 21 | * 22 | * @param {string} githubUsername 23 | * @param {slug: string, profile:string} data 24 | * @return {Promise} 25 | */ 26 | async function cacheData( githubUsername, data ) { 27 | const cache = await self.caches.open( 'wordpressers-on-github' ); 28 | const req = new Request( 29 | `https://profiles.wordpress.org/wp-json/wporg-github/v1/lookup/${ githubUsername }` 30 | ); 31 | let expireTime = Date.now() + 60 * 60 * 1000; 32 | if ( data ) { 33 | expireTime = Date.now() + 24 * 60 * 60 * 1000; 34 | } 35 | const resp = { 36 | data, 37 | expire: expireTime, 38 | }; 39 | await cache.put( req, new Response( JSON.stringify( resp ) ) ); 40 | } 41 | 42 | async function getUserFromCache( githubUsername ) { 43 | const result = await checkCache( githubUsername ); 44 | if ( result ) { 45 | const ttl = result.expire - Date.now(); 46 | if ( ttl > 0 ) { 47 | return result.data; 48 | } 49 | } 50 | return null; 51 | } 52 | 53 | /** 54 | * Fetch user information from WordPress.org API. 55 | * 56 | * @param {string} githubUsername 57 | * @returns {Promise<{slug: string, profile: string}|{code: string, message: string}>} 58 | */ 59 | async function getUserRemote( githubUsername ) { 60 | const url = `https://profiles.wordpress.org/wp-json/wporg-github/v1/lookup/${ githubUsername }`; 61 | const res = await fetch( url, { credentials: 'include' } ); 62 | if ( ! res.ok ) { 63 | throw new Error( 'Unable to lookup GitHub user login.' ); 64 | } 65 | 66 | /** 67 | * API result. 68 | * 69 | * @type {{slug: string, profile:string}|{code: string, message:string}} 70 | */ 71 | const data = await res.json(); 72 | 73 | if ( ! data.slug ) { 74 | return null; 75 | } 76 | 77 | return data; 78 | } 79 | 80 | async function getUser( githubUsername, sendResponse ) { 81 | if ( ! githubUsername ) { 82 | sendResponse( null ); 83 | return; 84 | } 85 | 86 | const cachedResult = await getUserFromCache( githubUsername ); 87 | if ( cachedResult ) { 88 | sendResponse( cachedResult ); 89 | return; 90 | } 91 | 92 | const data = await getUserRemote( githubUsername ); 93 | await cacheData( githubUsername, data || null ); 94 | sendResponse( data ); 95 | } 96 | 97 | chrome.runtime.onMessage.addListener( ( message, sender, sendResponse ) => { 98 | if ( ! message || ! message.type ) { 99 | return false; 100 | } 101 | 102 | if ( message.type === 'getUser' ) { 103 | void getUser( message.githubUsername, sendResponse ); 104 | return true; 105 | } 106 | 107 | return false; 108 | } ); 109 | -------------------------------------------------------------------------------- /chrome/src/content-script.js: -------------------------------------------------------------------------------- 1 | const HAS_VISITED = 'wogHasVisited'; 2 | 3 | window.addEventListener( 'load', async () => { 4 | const observer = new MutationObserver( async ( mutations ) => { 5 | for ( const mutation of mutations ) { 6 | for ( const node of mutation.addedNodes ) { 7 | if ( node.nodeType !== Node.ELEMENT_NODE ) { 8 | continue; 9 | } 10 | await findAndAddBadges( node ); 11 | await addVcard( document.documentElement ); 12 | } 13 | } 14 | } ); 15 | observer.observe( document.documentElement, { 16 | childList: true, 17 | subtree: true, 18 | } ); 19 | 20 | await findAndAddBadges( document.documentElement ); 21 | await addVcard( document.documentElement ); 22 | } ); 23 | 24 | async function addVcard( root ) { 25 | const vcardList = root.querySelector( 'ul.vcard-details' ); 26 | if ( ! vcardList ) { 27 | return; 28 | } 29 | 30 | const vcardUsername = root.querySelector( '.vcard-username' ); 31 | if ( ! vcardUsername ) { 32 | return; 33 | } 34 | 35 | const matches = vcardUsername.innerText.match( /[a-z0-9-]+/ ); 36 | 37 | if ( ! matches ) { 38 | return; 39 | } 40 | 41 | const githubUsername = matches[ 0 ]; 42 | 43 | const result = await lookupUser( githubUsername ); 44 | if ( ! result || ! result.profile ) { 45 | return; 46 | } 47 | 48 | const vcardDetail = document.createElement( 'li' ); 49 | vcardDetail.setAttribute( 'itemprop', 'social' ); 50 | vcardDetail.classList.add( 'vcard-detail', 'pt-1' ); 51 | 52 | const mark = document.createElement( 'img' ); 53 | mark.src = chrome.runtime.getURL( 'images/wp-logo.png' ); 54 | mark.width = 16; 55 | mark.height = 16; 56 | mark.classList.add( 'octicon' ); 57 | vcardDetail.appendChild( mark ); 58 | 59 | const profileLink = document.createElement( 'a' ); 60 | profileLink.target = '_blank'; 61 | profileLink.setAttribute( 'href', result.profile ); 62 | profileLink.textContent = `@${ result.slug }`; 63 | profileLink.classList.add( 'Link--primary' ); 64 | vcardDetail.appendChild( profileLink ); 65 | 66 | if ( vcardList.dataset[ HAS_VISITED ] ) { 67 | return; 68 | } 69 | 70 | vcardList.appendChild( vcardDetail ); 71 | 72 | vcardList.dataset[ HAS_VISITED ] = 'true'; 73 | } 74 | 75 | async function findAndAddBadges( root ) { 76 | const loginMap = findGitHubLogins( root ); 77 | const promises = []; 78 | for ( const login of Object.keys( loginMap ) ) { 79 | promises.push( addWordPressLogos( login, loginMap[ login ] ) ); 80 | } 81 | 82 | await Promise.all( promises ); 83 | } 84 | 85 | function findGitHubLogins( element ) { 86 | const loginMap = {}; 87 | const cards = element.querySelectorAll( '[data-hovercard-type="user"]' ); 88 | for ( const c of cards ) { 89 | if ( c.dataset[ HAS_VISITED ] ) { 90 | continue; 91 | } 92 | 93 | if ( ! c.textContent || c.textContent.trim().length === 0 ) { 94 | continue; 95 | } 96 | 97 | const url = c.dataset[ 'hovercardUrl' ]; 98 | if ( ! url ) { 99 | continue; 100 | } 101 | 102 | const regex = new RegExp( '^/users/(.*)/hovercard' ); 103 | if ( ! regex.test( url ) ) { 104 | continue; 105 | } 106 | 107 | const result = regex.exec( url ); 108 | if ( ! result ) { 109 | continue; 110 | } 111 | 112 | const login = result[ 1 ].trim(); 113 | if ( ! login ) { 114 | continue; 115 | } 116 | 117 | if ( ! loginMap[ login ] ) { 118 | loginMap[ login ] = []; 119 | } 120 | 121 | loginMap[ login ].push( c ); 122 | } 123 | 124 | return loginMap; 125 | } 126 | 127 | async function addWordPressLogos( githubUsername, elements ) { 128 | const result = await lookupUser( githubUsername ); 129 | if ( ! result || ! result.profile ) { 130 | return; 131 | } 132 | 133 | for ( const e of elements ) { 134 | addWordPressLogo( result, e ); 135 | } 136 | } 137 | 138 | /** 139 | * Add a WordPress logo to the given element. 140 | * 141 | * @param {{slug: string, profile:string}} result 142 | * @param {HTMLElement} element 143 | */ 144 | function addWordPressLogo( result, element ) { 145 | if ( element.dataset[ HAS_VISITED ] ) { 146 | return; 147 | } 148 | 149 | const profileLink = document.createElement( 'a' ); 150 | profileLink.target = '_blank'; 151 | profileLink.setAttribute( 'href', result.profile ); 152 | profileLink.classList.add( 'tooltipped', 'tooltipped-s' ); 153 | profileLink.setAttribute( 'aria-label', result.slug ); 154 | profileLink.style.paddingLeft = '2px'; 155 | 156 | const mark = document.createElement( 'img' ); 157 | mark.src = chrome.runtime.getURL( 'images/wp-logo.png' ); 158 | mark.width = 12; 159 | mark.height = 12; 160 | mark.style.verticalAlign = 'middle'; 161 | profileLink.appendChild( mark ); 162 | 163 | if ( ! element.checkVisibility || element.checkVisibility() ) { 164 | if ( element.tagName === 'A' ) { 165 | profileLink.style.paddingRight = window 166 | .getComputedStyle( element, null ) 167 | .getPropertyValue( 'padding-right' ); 168 | 169 | if ( profileLink.style.paddingRight !== '0px' ) { 170 | profileLink.style.paddingLeft = '0px'; 171 | } 172 | 173 | element.insertAdjacentElement( 'afterend', profileLink ); 174 | } else { 175 | element.appendChild( profileLink ); 176 | } 177 | } 178 | 179 | element.dataset[ HAS_VISITED ] = 'true'; 180 | } 181 | 182 | /** 183 | * Look up a user by sending its information to the background script. 184 | * 185 | * @param {string} githubUsername 186 | * @return {Promise<{slug: string, url: string} | null>} 187 | */ 188 | async function lookupUser( githubUsername ) { 189 | const msg = { 190 | type: 'getUser', 191 | githubUsername, 192 | }; 193 | return chrome.runtime.sendMessage( msg ); 194 | } 195 | -------------------------------------------------------------------------------- /firefox/_locales/en/messages.json: -------------------------------------------------------------------------------- 1 | { 2 | "appTitle": { 3 | "message": "WordPressers on GitHub" 4 | }, 5 | "appDescription": { 6 | "message": "Shows WordPress.org profiles for users on GitHub." 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /firefox/images/wp-128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swissspidy/wordpressers-on-github/23bb4ce61f357ea33a831857d337bc83529e086c/firefox/images/wp-128.png -------------------------------------------------------------------------------- /firefox/images/wp-16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swissspidy/wordpressers-on-github/23bb4ce61f357ea33a831857d337bc83529e086c/firefox/images/wp-16.png -------------------------------------------------------------------------------- /firefox/images/wp-32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swissspidy/wordpressers-on-github/23bb4ce61f357ea33a831857d337bc83529e086c/firefox/images/wp-32.png -------------------------------------------------------------------------------- /firefox/images/wp-48.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swissspidy/wordpressers-on-github/23bb4ce61f357ea33a831857d337bc83529e086c/firefox/images/wp-48.png -------------------------------------------------------------------------------- /firefox/images/wp-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/swissspidy/wordpressers-on-github/23bb4ce61f357ea33a831857d337bc83529e086c/firefox/images/wp-logo.png -------------------------------------------------------------------------------- /firefox/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "manifest_version": 2, 3 | "name": "WordPressers on GitHub", 4 | "version": "1.0.1", 5 | "description": "Show WordPress.org profiles for users on GitHub.", 6 | "default_locale": "en", 7 | "browser_specific_settings": { 8 | "gecko": { 9 | "id": "wordpressers-on-github@pascalbirchler.com" 10 | } 11 | }, 12 | "permissions": [ 13 | "storage", 14 | "https://github.com/*", 15 | "https://profiles.wordpress.org/*" 16 | ], 17 | "background": { 18 | "scripts": [ "src/background.js" ], 19 | "type": "module" 20 | }, 21 | "content_scripts": [ 22 | { 23 | "matches": [ "https://github.com/*" ], 24 | "js": [ "src/content-script.js" ] 25 | } 26 | ], 27 | "web_accessible_resources": [ 28 | "images/wp-logo.png" 29 | ], 30 | "icons": { 31 | "16": "/images/wp-16.png", 32 | "32": "/images/wp-32.png", 33 | "48": "/images/wp-48.png", 34 | "128": "/images/wp-128.png" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /firefox/src/background.js: -------------------------------------------------------------------------------- 1 | async function checkCache( githubUsername ) { 2 | const cache = await self.caches.open( 'wordpressers-on-github' ); 3 | const req = new Request( 4 | `https://profiles.wordpress.org/wp-json/wporg-github/v1/lookup/${ githubUsername }` 5 | ); 6 | const response = await cache.match( req ); 7 | if ( response ) { 8 | return response.json(); 9 | } 10 | return null; 11 | } 12 | 13 | /** 14 | * Store data in cache. 15 | * 16 | * @param {string} githubUsername 17 | * @param {slug: string, profile:string} data 18 | * @return {Promise} 19 | */ 20 | async function cacheData( githubUsername, data ) { 21 | const cache = await self.caches.open( 'wordpressers-on-github' ); 22 | const req = new Request( 23 | `https://profiles.wordpress.org/wp-json/wporg-github/v1/lookup/${ githubUsername }` 24 | ); 25 | let expireTime = Date.now() + 60 * 60 * 1000; 26 | if ( data ) { 27 | expireTime = Date.now() + 24 * 60 * 60 * 1000; 28 | } 29 | const resp = { 30 | data, 31 | expire: expireTime, 32 | }; 33 | await cache.put( req, new Response( JSON.stringify( resp ) ) ); 34 | } 35 | 36 | async function getUserFromCache( githubUsername ) { 37 | const result = await checkCache( githubUsername ); 38 | if ( result ) { 39 | const ttl = result.expire - Date.now(); 40 | if ( ttl > 0 ) { 41 | return result.slug; 42 | } 43 | } 44 | return null; 45 | } 46 | 47 | /** 48 | * Fetch user information from WordPress.org API. 49 | * 50 | * @param {string} githubUsername 51 | * @returns {Promise<{slug: string, profile: string}|{code: string, message: string}>} 52 | */ 53 | async function getUserRemote( githubUsername ) { 54 | const url = `https://profiles.wordpress.org/wp-json/wporg-github/v1/lookup/${ githubUsername }`; 55 | const res = await fetch( url, { credentials: 'include', mode: 'cors' } ); 56 | if ( ! res.ok ) { 57 | throw new Error( 'Unable to lookup GitHub user login.' ); 58 | } 59 | 60 | /** 61 | * API result. 62 | * 63 | * @type {{slug: string, profile:string}|{code: string, message:string}} 64 | */ 65 | const data = await res.json(); 66 | 67 | if ( ! data.slug ) { 68 | return null; 69 | } 70 | 71 | return data; 72 | } 73 | 74 | async function getUser( githubUsername ) { 75 | if ( ! githubUsername ) { 76 | return null; 77 | } 78 | 79 | const cachedResult = await getUserFromCache( githubUsername ); 80 | if ( cachedResult ) { 81 | return cachedResult; 82 | } 83 | 84 | const data = await getUserRemote( githubUsername ); 85 | await cacheData( githubUsername, data || null ); 86 | return data; 87 | } 88 | 89 | browser.runtime.onMessage.addListener( ( message ) => { 90 | if ( ! message || ! message.type ) { 91 | return undefined; 92 | } 93 | 94 | if ( message.type === 'getUser' ) { 95 | return getUser( message.githubUsername ); 96 | } 97 | 98 | return undefined; 99 | } ); 100 | -------------------------------------------------------------------------------- /firefox/src/content-script.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @type {browser.permissions.Permissions} permissions 3 | */ 4 | //let permissions = { 5 | // origins: [ 'https://github.com/*', 'https://profiles.wordpress.org/*' ], 6 | // permissions: [ 'storage' ], 7 | //}; 8 | // 9 | //const hasPermissions = await browser.permissions.contains( permissions ); 10 | //if ( ! hasPermissions ) { 11 | // await browser.permissions.request( permissions ); 12 | //} 13 | 14 | const HAS_VISITED = 'wogHasVisited'; 15 | 16 | async function init() { 17 | const observer = new MutationObserver( async ( mutations ) => { 18 | for ( const mutation of mutations ) { 19 | for ( const node of mutation.addedNodes ) { 20 | if ( node.nodeType !== Node.ELEMENT_NODE ) { 21 | continue; 22 | } 23 | await findAndAddBadges( node ); 24 | await addVcard( document.documentElement ); 25 | } 26 | } 27 | } ); 28 | 29 | observer.observe( document.documentElement, { 30 | childList: true, 31 | subtree: true, 32 | } ); 33 | 34 | await findAndAddBadges( document.documentElement ); 35 | await addVcard( document.documentElement ); 36 | } 37 | 38 | async function addVcard( root ) { 39 | const vcardList = root.querySelector( 'ul.vcard-details' ); 40 | if ( ! vcardList ) { 41 | return; 42 | } 43 | 44 | const vcardUsername = root.querySelector( '.vcard-username' ); 45 | if ( ! vcardUsername ) { 46 | return; 47 | } 48 | 49 | const matches = vcardUsername.innerText.match( /[a-z0-9-]+/ ); 50 | 51 | if ( ! matches ) { 52 | return; 53 | } 54 | 55 | const githubUsername = matches[ 0 ]; 56 | 57 | const result = await lookupUser( githubUsername ); 58 | if ( ! result || ! result.profile ) { 59 | return; 60 | } 61 | 62 | const vcardDetail = document.createElement( 'li' ); 63 | vcardDetail.setAttribute( 'itemprop', 'social' ); 64 | vcardDetail.classList.add( 'vcard-detail', 'pt-1' ); 65 | 66 | const mark = document.createElement( 'img' ); 67 | mark.src = chrome.runtime.getURL( 'images/wp-logo.png' ); 68 | mark.width = 16; 69 | mark.height = 16; 70 | mark.classList.add( 'octicon' ); 71 | vcardDetail.appendChild( mark ); 72 | 73 | const profileLink = document.createElement( 'a' ); 74 | profileLink.target = '_blank'; 75 | profileLink.setAttribute( 'href', result.profile ); 76 | profileLink.textContent = `@${ result.slug }`; 77 | profileLink.classList.add( 'Link--primary' ); 78 | vcardDetail.appendChild( profileLink ); 79 | 80 | if ( vcardList.dataset[ HAS_VISITED ] ) { 81 | return; 82 | } 83 | 84 | vcardList.appendChild( vcardDetail ); 85 | 86 | vcardList.dataset[ HAS_VISITED ] = 'true'; 87 | } 88 | 89 | if ( 'complete' === document.readyState ) { 90 | void init(); 91 | } else { 92 | window.addEventListener( 'load', async () => { 93 | await init(); 94 | } ); 95 | } 96 | 97 | async function findAndAddBadges( root ) { 98 | const loginMap = findGitHubLogins( root ); 99 | const promises = []; 100 | for ( const login of Object.keys( loginMap ) ) { 101 | promises.push( addWordPressLogos( login, loginMap[ login ] ) ); 102 | } 103 | 104 | await Promise.all( promises ); 105 | } 106 | 107 | function findGitHubLogins( element ) { 108 | const loginMap = {}; 109 | const cards = element.querySelectorAll( '[data-hovercard-type="user"]' ); 110 | for ( const c of cards ) { 111 | if ( c.dataset[ HAS_VISITED ] ) { 112 | continue; 113 | } 114 | 115 | if ( ! c.textContent || c.textContent.trim().length === 0 ) { 116 | continue; 117 | } 118 | 119 | const url = c.dataset[ 'hovercardUrl' ]; 120 | if ( ! url ) { 121 | continue; 122 | } 123 | 124 | const regex = new RegExp( '^/users/(.*)/hovercard' ); 125 | if ( ! regex.test( url ) ) { 126 | continue; 127 | } 128 | 129 | const result = regex.exec( url ); 130 | if ( ! result ) { 131 | continue; 132 | } 133 | 134 | const login = result[ 1 ].trim(); 135 | if ( ! login ) { 136 | continue; 137 | } 138 | 139 | if ( ! loginMap[ login ] ) { 140 | loginMap[ login ] = []; 141 | } 142 | 143 | loginMap[ login ].push( c ); 144 | } 145 | 146 | return loginMap; 147 | } 148 | 149 | async function addWordPressLogos( githubUsername, elements ) { 150 | const result = await lookupUser( githubUsername ); 151 | if ( ! result || ! result.profile ) { 152 | return; 153 | } 154 | 155 | for ( const e of elements ) { 156 | addWordPressLogo( result, e ); 157 | } 158 | } 159 | 160 | /** 161 | * Add a WordPress logo to the given element. 162 | * 163 | * @param {{slug: string, profile:string}} result 164 | * @param {HTMLElement} element 165 | */ 166 | function addWordPressLogo( result, element ) { 167 | if ( element.dataset[ HAS_VISITED ] ) { 168 | return; 169 | } 170 | 171 | const profileLink = document.createElement( 'a' ); 172 | profileLink.target = '_blank'; 173 | profileLink.setAttribute( 'href', result.profile ); 174 | profileLink.classList.add( 'tooltipped', 'tooltipped-s' ); 175 | profileLink.setAttribute( 'aria-label', result.slug ); 176 | profileLink.style.paddingLeft = '2px'; 177 | 178 | const mark = document.createElement( 'img' ); 179 | mark.src = browser.runtime.getURL( 'images/wp-logo.png' ); 180 | mark.width = 12; 181 | mark.height = 12; 182 | mark.style.verticalAlign = 'middle'; 183 | profileLink.appendChild( mark ); 184 | 185 | if ( ! element.checkVisibility || element.checkVisibility() ) { 186 | if ( element.tagName === 'A' ) { 187 | profileLink.style.paddingRight = window 188 | .getComputedStyle( element, null ) 189 | .getPropertyValue( 'padding-right' ); 190 | 191 | if ( profileLink.style.paddingRight !== '0px' ) { 192 | profileLink.style.paddingLeft = '0px'; 193 | } 194 | 195 | element.insertAdjacentElement( 'afterend', profileLink ); 196 | } else { 197 | element.appendChild( profileLink ); 198 | } 199 | } 200 | 201 | element.dataset[ HAS_VISITED ] = 'true'; 202 | } 203 | 204 | /** 205 | * Look up a user by sending its information to the background script. 206 | * 207 | * @param {string} githubUsername 208 | * @return {Promise<{slug: string, url: string} | null>} 209 | */ 210 | async function lookupUser( githubUsername ) { 211 | const msg = { 212 | type: 'getUser', 213 | githubUsername, 214 | }; 215 | return browser.runtime.sendMessage( msg ); 216 | } 217 | --------------------------------------------------------------------------------