Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using begin and end in JavaScript

Tags:

javascript

Can I somehow change the JavaScript language such that the word end equates to the } symbol and begin equates to {?

like image 496
Phillip Senn Avatar asked Dec 11 '22 16:12

Phillip Senn


1 Answers

No, you can't. The best you could do is have a script that modifies another script with an invalid type and changes it to a valid type so you'd get the effect, kind of.

<script type="text/x-algolscript">
    function hello() begin
        alert("Hello, world!");
    end
    hello();
</script>
<script type="text/javascript">
    Array.prototype.forEach.call(document.getElementsByTagName('script'), function(script) {
        if(script.type === 'text/x-algolscript') {
            var oldParent = script.parentNode;
            var oldNext = script.nextSibling;
            oldParent.removeChild(script);
            script.textContent = script.textContent.replace(/\bbegin\b/g, '{').replace(/\bend\b/g, '}');
            script.type = 'text/javascript';
            oldParent.insertBefore(script, oldNext);
        }
    });
</script>

This is context-insensitive, however, and will gladly change your strings and such.

Bonus: Minified and more browser-compatible version:

!function(s,i,t,e,l,p,o,n){for(l=s.length;i<l;i++)((e=s[i]).type==='text/x-algolscript')&&t.push(e);for(i=0;i<t.length;i++)o=(e=t[i]).parentNode,n=e.nextSibling,o.removeChild(e),p='textContent',e[p]||(p='innerText'),e[p]=e[p].replace(/\bbegin\b/,'{').replace(/\bend\b/,'}'),e.type='text/javascript',o.insertBefore(e,n)}(document.getElementsByTagName('script'),0,[]);
like image 123
icktoofay Avatar answered Jan 02 '23 10:01

icktoofay