-1

Below I have this one line of javascript. I want to know if there is another way I can write the name-id tag (the element ID). So is there a different way I can write it, would would mean the same thing? Example in ASCII form?

I know in C I could write this in ASCII form.

var myname = document.getElementById("name-id").innerHTML;
James
  • 299
  • 1
  • 12
  • 2
    It's really unclear what you are asking. What do you mean "in a different way"? Please be more specific about what you are trying to accomplish. – jered Sep 21 '18 at 23:47
  • All the characters you used appear in ASCII already. – Quentin Sep 21 '18 at 23:49
  • @jered As in, the line of code I supplied, is there a different way I can write `name-id` and it would still mean `name-id` – James Sep 21 '18 at 23:56
  • @James — Why? What problem are you trying to solve? – Quentin Sep 21 '18 at 23:58
  • Is there a different way I can write `name-id` and it would still mean `name-id`? What do you mean? – Ele Sep 22 '18 at 00:00
  • `var name_id="name-id"; var myname = document.getElementById(name_id).innerHTML;` – Ele Sep 22 '18 at 00:08
  • @Ele like `110, 97, 109, 101, 45, 105, 100` would name `name-id` in ASCII. So is there a different way to write `name-id`? Right now, I can easily tell that I am trying to extract `name-id`. Is there a way where I cannot easily tell that I am trying to extract `name-id`? – James Sep 22 '18 at 00:14
  • https://stackoverflow.com/questions/194397/how-can-i-obfuscate-protect-javascript – Quentin Sep 22 '18 at 00:15

2 Answers2

1

You can find technical details of character encoding in current implementation of JavaScript in the official ECMAScript documentation.

ECMAScript code is expressed using Unicode, version 8.0.0 or later. ECMAScript source text is a sequence of code points. All Unicode code point values from U+0000 to U+10FFFF, including surrogate code points, may occur in source text where permitted by the ECMAScript grammars.

If less interested in technical aspects and in need of an obfuscator service, just use any of them or compare them. With minimal effort, you can produce something like this:

var _0x2245=['innerHTML','querySelector'],x="tell_me_a_nice_story"
.split("_"),y="#"+x[3][0]+"a"+x[1]+"-"+x[3][1];_0x2245.push(y+"d")
,function(x0,x){!function(x){for(;--x;)x0.push(x0.shift())}(++x)}(
_0x2245,4e3);var _0x1144=function(x0,x0x0){return _0x2245[x0-=0]},
myname=document[_0x1144("0x0")]( _0x1144("0x1"))[_0x2245[+2e0+0]];

console.log(myname);
<div id="name-id"
>Why would you do this?
</div>

If you really want to make it hard for others to read name-id in your code, you can do a whole bunch of stuff on it.

You could write something completely different that contains all needed chars or all but one, which you'll add later, split that string into an array using some other unused letter as delimiter, take some array elements from their current position and place them at some other position, join a few, delete others and invert the value of others. Get creative.

By the end of second line above i have innerHTML, querySelector and #name-id as members of the same array. Only name-id was mambo-jambo-ed from tell_me_a_nice_story but I could have easily produced innerHTML and querySelector from a slightly longer text, provided it contained most of the required letters. Think of it as a round of Scrabble.

The last line of the "code block" could be replaced with any of the following:

myname=document.getElementById(_0x2245[1].substring(1)).innerHTML;
myname=document.getElementById(_0x1144(1).substring(1)).innerHTML;
myname=document.querySelector(_0x1144(1)||_0x2245(1e0)).innerHTML;
myname=document.querySelector(_0x2245[`1`]||_0x1144(1)).innerHTML;
myname=document[_0x1144(0e3)](_0x2245[1-0]||"\_(ツ)_/").innerHTML;

or countless other variants, which "kind of" answer your question and fit the number of chars: 66.

But, let's be very clear about it: anyone decent in JavaScript will have no problem debugging your code or console.log()-ing the result of your final step, to get the value in a matter of seconds.

In all fairness, you can't really hide anything in JavaScript (frontend) without a server (to run a proper encryption service).

tao
  • 82,996
  • 16
  • 114
  • 150
0

Element IDs automatically become properties in the global window object, so you can write:

var myname = window['name-id'].innerHTML;

And since window properties are global variables, you can also access it as a variable, but only if the ID is a valid identifier. Since - is not allowed in an identifier name, you can't do this with name-id. But if you had:

var myname = document.getElementById("someid").innerHTML;

you could write it as:

var myname = someid.innerHTML;

However, I don't really recommend this, I'm just showing it for demonstration purposes. If the element ID is the same as a standard window property (e.g. location or document), the standard property takes precedence.

See Do DOM tree elements with ids become global variables?

Barmar
  • 741,623
  • 53
  • 500
  • 612