Skip to content

Commit 7f8f417

Browse files
committed
Put in some documentation and changed references to zero-length to zero-width. Resolves #4 where the warning was obscuring the text.
1 parent af755ba commit 7f8f417

File tree

6 files changed

+79
-42
lines changed

6 files changed

+79
-42
lines changed

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Detect Zero-Width Characters",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"manifest_version": 2,
55
"description": "Detects zero-width characters on a page, highlights the containing DOM element, and displays a warning at bottom-right of element.",
66
"homepage_url": "https://github.com/roymckenzie/detect-zero-width-characters-chrome-extension",

src/background/background.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
11
(function() {
22
let contextMenuOptionId, selectionText;
33

4+
/**
5+
* Sanitizes and copies some text.
6+
*
7+
* @since 0.0.2
8+
*/
49
const sanitizeAndCopy = function() {
5-
copyTextToClipboard(sanitize(selectionText));
10+
copyTextToClipboard( sanitize( selectionText ) );
611
};
712

8-
const handleContextMenu = function(request) {
9-
if (request.shouldSanitizeSelection) {
13+
/**
14+
* Builds a context menu in Chrome.
15+
*
16+
* @param {object} request The chrome.runtime.onMessage object.
17+
*
18+
* @since 0.0.2
19+
*/
20+
const handleContextMenu = function( request ) {
21+
if ( request.shouldSanitizeSelection ) {
1022
selectionText = request.selection;
1123

12-
if (!contextMenuOptionId) {
24+
if ( !contextMenuOptionId ) {
1325
contextMenuOptionId = chrome.contextMenus.create({
1426
"title" : "Sanitize and copy",
1527
"type" : "normal",
16-
"contexts" : ["selection"],
28+
"contexts" : [ "selection" ],
1729
"onclick" : sanitizeAndCopy
1830
});
1931
}
2032
} else {
21-
if (contextMenuOptionId) {
22-
chrome.contextMenus.remove(contextMenuOptionId);
33+
if ( contextMenuOptionId ) {
34+
chrome.contextMenus.remove( contextMenuOptionId );
2335
contextMenuOptionId = null;
2436
}
2537
}
2638
};
2739

28-
chrome.runtime.onMessage.addListener(handleContextMenu);
29-
})();
40+
chrome.runtime.onMessage.addListener( handleContextMenu );
41+
})();

src/constants.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
const zeroLengthCharacterCodes = [ 8203, 8204, 8205, 8288 ];
1+
/**
2+
* The different zero-width character codes.
3+
*
4+
* @since 0.0.1
5+
*/
6+
const zeroWidthCharacterCodes = [ 8203, 8204, 8205, 8288 ];

src/inject/inject.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
.zero-length-characters {
1+
.zero-width-characters {
22
background-color: rgba(255, 0, 0, 0.2) !important;
33
position: relative;
44
}
55

6-
.zero-length-characters:after {
6+
.zero-width-characters:after {
77
background: #ffb2b2;
8-
bottom: 0;
8+
bottom: -14px;
99
color: #000;
1010
content: 'Warning: Contains zero-width characters.';
1111
float: right;

src/inject/inject.js

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
(function() {
2-
let elementsWithZLCC = [];
3-
4-
const checkPage = function() {
5-
const allElements = document.getElementsByTagName('*');
6-
[...allElements].forEach( checkElement );
7-
}
8-
2+
let elementsWithZWCC = [];
3+
4+
/**
5+
* Checks DOM element for zero-width character.
6+
*
7+
* @param {dom node} element A DOM node.
8+
*
9+
* @since 0.0.1
10+
*/
911
// From: https://jsfiddle.net/tim333/np874wae/13/
1012
const checkElement = function( element ) {
1113
const text = textWithoutChildren( element );
@@ -14,18 +16,27 @@
1416
unicodeCode = character.codePointAt(0);
1517

1618
if (
17-
zeroLengthCharacterCodes.includes( unicodeCode )
18-
&& !elementsWithZLCC.includes(element)
19+
zeroWidthCharacterCodes.includes( unicodeCode )
20+
&& !elementsWithZWCC.includes(element)
1921
) {
20-
elementsWithZLCC.push(element)
22+
elementsWithZWCC.push(element)
2123
}
2224
});
2325

24-
elementsWithZLCC.forEach(function( element ) {
25-
element.classList.add('zero-length-characters');
26+
elementsWithZWCC.forEach(function( element ) {
27+
element.classList.add('zero-width-characters');
2628
})
2729
}
2830

31+
/**
32+
* Pulls text from DOM node not including child DOM nodes.
33+
*
34+
* @param {node} element A DOM node.
35+
*
36+
* @since 0.0.1
37+
*
38+
* @return {string} The text inside the DOM node.
39+
*/
2940
// From: https://stackoverflow.com/a/9340862/535363
3041
const textWithoutChildren = function( element ) {
3142
let child = element.firstChild,
@@ -41,6 +52,16 @@
4152
return texts.join("");
4253
}
4354

55+
/**
56+
* Checks current document for zero-width characters.
57+
*
58+
* @since 0.0.1
59+
*/
60+
const checkPage = function() {
61+
const allElements = document.getElementsByTagName('*');
62+
[...allElements].forEach( checkElement );
63+
}
64+
4465
chrome.extension.sendMessage({}, function(response) {
4566
var readyStateCheckInterval = setInterval(function() {
4667
if (document.readyState === "complete") {
@@ -59,10 +80,10 @@
5980
}, 10);
6081
});
6182

62-
document.body.addEventListener('mouseup', function (e) {
83+
document.body.addEventListener('mouseup', function ( event ) {
6384
const selection = window.getSelection();
6485

65-
const shouldSanitizeSelection = elementsWithZLCC
86+
const shouldSanitizeSelection = elementsWithZWCC
6687
.map(function(element) {
6788
return selection.containsNode(element, true);
6889
})
@@ -73,16 +94,16 @@
7394
"shouldSanitizeSelection": shouldSanitizeSelection,
7495
"selection": selection.toString()
7596
});
76-
} catch(e) {
97+
} catch(event) {
7798
if (
78-
e.message.match(/Invocation of form runtime\.connect/) &&
79-
e.message.match(/doesn't match definition runtime\.connect/)
99+
event.message.match(/Invocation of form runtime\.connect/) &&
100+
event.message.match(/doesn't match definition runtime\.connect/)
80101
) {
81102
console.error('Chrome extension has been reloaded. Please refresh the page');
82103
} else {
83-
throw(e);
104+
throw(event);
84105
}
85106
}
86107
});
87108

88-
})();
109+
})();

src/utils.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
const sanitize = function(text) {
2-
return [...text].filter(function(char) {
3-
const unicodeCode = char.codePointAt(0);
4-
5-
return !zeroLengthCharacterCodes.includes(unicodeCode);
6-
}).join("");
1+
const sanitize = function( text ) {
2+
return [...text].filter( function( char ) {
3+
const unicodeCode = char.codePointAt(0);
4+
return !zeroWidthCharacterCodes.includes( unicodeCode );
5+
}).join("");
76
}
87

98
//https://stackoverflow.com/a/18455088/6591929
109
const copyTextToClipboard = function (text) {
1110
const copyFrom = document.createElement("textarea");
1211
const body = document.body;
1312
copyFrom.textContent = text;
14-
body.appendChild(copyFrom);
13+
body.appendChild( copyFrom );
1514
copyFrom.select();
1615
document.execCommand('copy');
17-
body.removeChild(copyFrom);
18-
}
16+
body.removeChild( copyFrom );
17+
}

0 commit comments

Comments
 (0)