Skip to content

Commit bc9181c

Browse files
authored
GitHub image preview: better compatibility with other extensions (#171)
1 parent 764b02e commit bc9181c

File tree

1 file changed

+9
-20
lines changed

1 file changed

+9
-20
lines changed

github-image-preview.user.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ==UserScript==
22
// @name GitHub Image Preview
3-
// @version 2.0.6
3+
// @version 2.0.7
44
// @description A userscript that adds clickable image thumbnails
55
// @license MIT
66
// @author Rob Garrison
@@ -29,8 +29,9 @@
2929
background-color:transparent !important; }
3030
.ghip-show-previews .Box-row:not(.ghsc-header):not(.hidden) > div[role] {
3131
display:none !important; }
32-
.ghip-wrapper.ghip-show-previews img.ghip-non-image { height:80px;
33-
margin-top:15px; opacity:.2; }
32+
.ghip-wrapper.ghip-show-previews svg.ghip-non-image,
33+
.ghip-wrapper.ghip-show-previews img.ghip-non-image { height:80px; width:80px;
34+
margin-top:15px; }
3435
.ghip-wrapper.ghip-show-previews .image { width:100%; position:relative;
3536
overflow:hidden; text-align:center; }
3637
@@ -185,7 +186,7 @@
185186
let content = "";
186187
// not every submodule includes a link; reference examples from
187188
// see https://github.com/electron/electron/tree/v1.1.1/vendor
188-
const el = $("a", row) || $("div[role='rowheader'] span[title]", row);
189+
const el = $("div[role='rowheader'] a, div[role='rowheader'] span[title]", row);
189190
const url = el && el.nodeName === "A" ? el.href : "";
190191
// use innerHTML because some links include path - see "third_party/lss"
191192
const fileName = el && el.textContent.trim() || "";
@@ -215,28 +216,16 @@
215216
content = updateTemplate(url, `${title()}${svgPlaceholder(url)}`);
216217
} else {
217218
// *** non-images (file/folder icons) ***
218-
const svg = $("svg", row);
219+
const svg = $("[role='gridcell'] svg, [role='gridcell'] img", row);
219220
if (svg) {
220221
// non-files svg class: "directory", "submodule" or "symlink"
221222
// add "ghip-folder" class for file-filters userscript
222223
const noExt = svg.matches(folderIconClasses) ? " ghip-folder" : "";
223-
// add xmlns otherwise the svg won't work inside an img
224-
// GitHub doesn't include this attribute on any svg octicons
225-
const svgHTML = svg.outerHTML.replace("<svg", "<svg xmlns='http://www.w3.org/2000/svg'");
224+
const clone = svg.cloneNode(true);
225+
clone.classList.add("ghip-non-image");
226226
// include "leaflet-tile-container" to invert icon for GitHub-Dark
227227
content = `${title("non-image")}<span class="leaflet-tile-container${noExt}">` +
228-
`<img class="ghip-non-image" src="data:image/svg+xml;base64,` +
229-
window.btoa(svgHTML) + `"/>`;
230-
// get file name + extension
231-
const str = url.substring(url.lastIndexOf("/") + 1, url.length);
232-
// don't include extension for folders, or files with no extension,
233-
// or files starting with a "." (e.g. ".gitignore")
234-
content += (!noExt && str.indexOf(".") > 0) ?
235-
"<h4 class='ghip-file-type'>" +
236-
str
237-
.substring(str.lastIndexOf(".") + 1, str.length)
238-
.toUpperCase() +
239-
"</h4></span>" : "</span>";
228+
clone.outerHTML + "</span>";
240229
content = url ?
241230
updateTemplate(url, content) :
242231
// empty url; use non-link template

0 commit comments

Comments
 (0)