Jump to content

[JS] UserScript / Chrome Browser Extension


Skitty
 Share

Recommended Posts

I've been trying to make an extension for embedding plain text links on imageboards like 4chan.

I've taken code from a more fully featured extension that has features that I really don't want because it's incompatible with the more popular extensions out there.

I have it working as an extension in my browser, the edited full version of it, but when I pack it for installation and re-install it, it throws errors like "Uncaught TypeError: Cannot read property 'links1' of undefined".

I don't know much about JS but I'm wondering why it's not throwing this error in the original version where it's edited but throws the error on the one installed from the packed crx file.

var links = /((?:https?|ftp)://[^s'"'<>()]+|www.[^s'"'<>()]+|[-w.+]+@(?:[-w]+.)+[w]{2,6})/gi;

function apply() {
        if (!window.location.href.match(/boards./)) return false;
        var a = document.getElementsByTagName("iframe"), b = document.getElementsByTagName("embed");
        Activate(a,b);
}

function Activate(a, b) {
        a = a ? a + " " : "";
        for (var d, c = xpath((a == "" ? "." : "id('" + a.substr(1, a.length - 2) + "')") + "//text()[ancestor::blockquote]"), e = 0; e < c.snapshotLength; e++) {
            d = c.snapshotItem(e);
            d != null && linkActivation(d);
        }
}

function linkActivation(a) {
        for (var b, d, c = a.textContent, e = null, g = 0, f = {}, youtube, vocaroo, soundcloud;
        (d = links.exec(c)) !== null;) if (!f[d[0]]) {
            f[d[0]] = true;
            null === e && (e = document.createElement("span"));
            b = d[0].replace(/.*$/, "");
            e.appendChild(document.createTextNode(c.substring(g, d.index)));
            if (!b.match(/.4chan.org/i)) {
                b = (youtube = b.match(/youtube.com/watch?.*?v=([a-z0-9-_#!]+)/i)) && (settings.youtube1 != "off" && !inThread || settings.youtube2 != "off" && inThread) ? '<iframe class="youtube-player" type="text/html" width="640" height="385" src="http://www.youtube.com/embed/' + youtube[1] + '?wmode=opaque" frameborder="0"></iframe><br /><a href="' + b + '" target="_blank">' + b + "</a>" : 
                (vocaroo = b.match(/vocaroo.com/(i/|?media=)([a-z0-9]+)/i)) && (settings.vocaroo1 != "off" && !inThread || settings.vocaroo2 != "off" && inThread) ? '<object type="application/x-shockwave-flash" style="width: 148px; height: 44px" data="http://vocaroo.com/player.swf?playMediaID=' + vocaroo[2] + '&server=m1.vocaroo.com&autoplay=0""><param name="movie" value="http://vocaroo.com/player.swf?playMediaID=' + vocaroo[2] + '&server=m1.vocaroo.com&autoplay=0"></object><br /><a href="' + b + '" target="_blank">' + b + "</a>" : 
                (soundcloud = b.match(/((www.)?soundcloud.com/([a-z0-9-_]+/?)+)/i)) && (settings.soundcloud1 != "off" && !inThread || settings.soundcloud2 != "off" && inThread) ? '<object height="81" width="400"><param name="movie" value="http://player.soundcloud.com/player.swf?url=http://' + soundcloud[1] + '&amp;g=bb"></param><param name="allowscriptaccess" value="always"></param><embed allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url=http://' + soundcloud[1] + '&amp;g=bb" type="application/x-shockwave-flash" width="400"></embed></object><br /><a href="http://' + soundcloud[1] + '" target="_blank">http://' + soundcloud[1] + "</a>" : 
                b.indexOf("@") > -1 && (settings.email1 != "off" && !inThread || settings.email2 != "off" && inThread) ? "<a href='mailto:" + b + "'>" + b + "</a>" : 
                settings.links1 != "off" && !inThread || settings.links2 != "off" && inThread && b.indexOf("@") < 0 ? "<a href='" + (b.match(/^http/i) ? b : "http://" + b) + "' target='_blank'>" + b + "</a>" : b;
                e.innerHTML = b;
                g = d.index + d[0].length;
            }
        }
        if (e) {
            e.appendChild(document.createTextNode(c.substring(g, c.length)));
            try {
                a.parentNode.replaceChild(e, a);
            } catch (h) {
                console.error(h);
            }
        }
}

function xpath(a) {
        return document.evaluate(a, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null)
}

apply();
Edited by CaptainClucks
Link to comment
Share on other sites

Th best I can grok is that settings is not defined within the linkActivation function. I'm no JS person at all but the very first line is (function () {, what's that all about?

Link to comment
Share on other sites

Th best I can grok is that settings is not defined within the linkActivation function. I'm no JS person at all but the very first line is (function () {, what's that all about?

It declares an anonymous function, wrapped in parenthesis so you can call it right away. This:

(function (s) {
alert(s);
})("hello");

would show you a 'hello' message box.

Edited by danielkza
Link to comment
Share on other sites

So, you don't look at the code in the OP and want to die?

I can write some very ugly AutoIt if you like... In fact name me any language and I'll make you look for a knife.

Not saying I like js... I just don't like it because it's used almost entirely for web development which I don't like. Same goes for php.

Link to comment
Share on other sites

One thing to note is that the original code that this came from was originally created in some other language that's called coffee something, I don't remember but I was reading about it and the dev seemed to mention that he converts the other language into this java script mess you see up there.

Link to comment
Share on other sites

I hate j&#097;v&#097;script so much. JS seems so tedious and ambiguous. Am I right?

I actually (mostly) like J&#097;v&#097;script as a language, ignoring the confusing web-browser stuff. I'm personally like writing code using anonymous and nested functions, closures, etc. It's very far from perfect though: the (lack of) a class system makes object-oriented programming much harder than it should be (that's one of the reasons CoffeeScript and other languages that compile to JS are getting popular), the weak typing system causes unexpected gotchas and weird behavior (I'm not even talking about the lack of static typing: Python has completely dynamic typing, but blows up whenever you try to mix up incompatible types instead of giving you retarded results), the (lack of proper) scoping is a mind-boggling oversight, etc.

One thing to note is that the original code that this came from was originally created in some other language that's called coffee something, I don't remember but I was reading about it and the dev seemed to mention that he converts the other language into this java script mess you see up there.

CoffeeScript is the name of the language you mention. Most of it translates in a straight-forward way to vanilla J&#097;v&#097;script: most of the converted code I see doesn't look as bad as this example. I wonder if it was actually bad CoffeeScript to begin with.

Edited by danielkza
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...