Rune Halvorsen avatar Rune Halvorsen committed 19db6b3

Initial add

Comments (0)

Files changed (5)

extension/config.xml

+<?xml version="1.0" encoding="utf-8"?>
+<widget xmlns="http://www.w3.org/ns/widgets">
+  <name>YayNay</name>
+  <description>
+    YayNay
+  </description>
+  <author href="http://bitbucket.org/runeh/" email="runefh@gmail.com">
+    Rune Halvorsen
+  </author>
+<!--
+  <icon src="unshorten_64.png" width="64" height="64"/>
+-->
+  <icon src="yaynay_18.png" width="18" height="18"/>
+
+<!--
+  <preference name="debug_mode" value="false"/>
+-->
+</widget>

extension/includes/yaynay.js

+// this is empty, but there must be an included js for the
+// getActiveTab call to succeed.

extension/index.html

+<!doctype html>
+<script>
+window.addEventListener("load", init, false);
+window.g_button = null;
+
+function init()
+{
+    opera.extension.onconnect = onConnectHandler;
+    opera.extension.tabs.onfocus = onFocus;
+
+    var props = {
+        disabled: false, // The button is enabled.
+        title: "Button example", // The tooltip title.
+        icon: "yaynay_18.png", // The icon (18x18) to use for the button.
+        onclick: toggleMood,
+        badge: {
+            display: "block",
+            textContent: "meh",
+            color: "white",
+            backgroundColor: "rgba(211, 0, 4, 1)"
+        }
+    };
+    window.g_button = opera.contexts.toolbar.createItem(props);
+    opera.contexts.toolbar.addItem(g_button);
+}
+
+function toggleMood(evt) {
+    var url = window.opera.extension.tabs.getFocused().url;
+    var currentStatus = getPageStatus(url);
+    var newStatus = null;
+    if (currentStatus == "yay") {
+        newStatus = "meh";
+    }
+    else if (currentStatus == "meh") {
+        newStatus = "nay";
+    }
+    else {
+        newStatus = "yay";
+    }
+
+    window.localStorage[url] = newStatus;
+    updateButton(url);
+}
+
+function onFocus(evt) {
+    updateButton();
+}
+
+function updateButton(url) {
+    url = url || window.opera.extension.tabs.getFocused().url;
+    var status = getPageStatus(url);
+    if (status == "meh") {
+        g_button.badge.display = "none";        
+    }
+    else {
+        g_button.badge.textContent = status;
+        g_button.badge.display = "block";
+    }
+}
+
+function onConnectHandler(event) {
+    updateButton(event.origin);
+}
+
+function getPageStatus(url) {
+    return window.localStorage[url] || "meh";
+}
+
+
+</script>

extension/options.html

+<!doctype html>
+<script src="includes/unshortinator.js"></script>
+<script>
+
+window.onload = function() {
+    setOptions();
+    updatePreview();
+    renderShortenerList();
+    var inputs = document.querySelectorAll("input");
+    for (var n=0, e; e=inputs[n]; n++) {
+        e.addEventListener("change", optionChange, false);
+    }
+    document.querySelector("#show_cache").addEventListener("click", renderCache, false);
+    document.querySelector("#about_button").addEventListener("click", showAbout, false);
+    document.querySelector("#clear_cache").addEventListener("click", clearCache, false);
+}
+
+function s2b(s) {
+    return s=="true" ? true : false;
+}
+
+function renderShortenerList() {
+    var cont = document.querySelector("#shorteners_list");
+    Unshortinator.shorteners.forEach(function(e) {
+        var a = document.createElement("a");
+        a.textContent = e;
+        a.setAttribute("href", "http://" + e);
+        var comma = document.createTextNode(", ");
+        cont.appendChild(a);
+        cont.appendChild(comma);
+    });
+    cont.removeChild(cont.childNodes[cont.childNodes.length-1]);
+}
+
+function setOptions() {
+    document.querySelector("#use_cache").checked = s2b(widget.preferences["use_cache"]);
+    document.querySelector("#rewrite_href").checked = s2b(widget.preferences["rewrite_href"]);
+    document.querySelector("#rewrite_textcontent").checked = s2b(widget.preferences["rewrite_textcontent"]);
+    document.querySelector("#add_title").checked = s2b(widget.preferences["add_title"]);
+    document.querySelector("#debug_mode").checked = s2b(widget.preferences["debug_mode"]);
+}
+
+function saveOptions() {
+    var options = getOptions();
+    widget.preferences["use_cache"] = options.use_cache;
+    widget.preferences["rewrite_href"] = options.rewrite_href;
+    widget.preferences["rewrite_textcontent"] = options.rewrite_textcontent;
+    widget.preferences["add_title"] = options.add_title;
+    widget.preferences["debug_mode"] =  options.debug_mode;
+}
+
+function showAbout(evt) {
+    document.querySelector("#about_box").style.display = "block";
+    var ele = evt.srcElement;
+    ele.parentNode.removeChild(ele);
+}
+
+function optionChange() {
+    saveOptions();
+    updatePreview();
+}
+
+function getOptions() {
+    return {
+        use_cache: document.querySelector("#use_cache").checked,
+        rewrite_href: document.querySelector("#rewrite_href").checked,
+        rewrite_textcontent: document.querySelector("#rewrite_textcontent").checked,
+        add_title: document.querySelector("#add_title").checked,
+        debug_mode: document.querySelector("#debug_mode").checked,
+    };
+}
+
+function updatePreview() {
+    var src = "http://bit.ly/d2Jarx";
+    var dst = "http://bitbucket.org/runeh/opera-unshorten";
+    var options = getOptions();
+    var elem = document.querySelector("#preview");
+    if (options.rewrite_textcontent) {
+        elem.textContent = dst;
+    }
+    else {
+        elem.textContent = src;
+    }
+
+    if (options.rewrite_href) {
+        elem.href = dst;
+    }
+    else {
+        elem.href = src;
+    }
+
+    if (options.add_title) {
+        elem.title = "title goes here " + dst;
+    }
+    else {
+        elem.title = "Title goes here";
+    }
+
+    if (options.debug_mode) {
+        elem.style.border = "solid thin red";
+    }
+    else {
+        elem.style.border = "";
+    }
+
+}
+
+function renderCache() {
+    var div = document.createElement("div");
+    var table = document.createElement("table");
+    var row = document.createElement("tr");
+    var th1 = document.createElement("th");
+    th1.textContent = "Source";
+    var th2 = document.createElement("th");
+    th2.textContent = "Destination";
+    row.appendChild(th1);
+    row.appendChild(th2);
+    table.appendChild(row);
+    var len =  window.localStorage.length;
+    var keys = [];
+    for (var n=0; n<len; n++) {
+        keys.push(window.localStorage.key(n));
+    }
+    keys.sort();
+    for (var n=0, src; src=keys[n]; n++) {
+        var row = document.createElement("tr");
+        var td1 = document.createElement("td");
+        td1.textContent = src;
+        var td2 = document.createElement("td");
+        td2.textContent = window.localStorage[src];
+        row.appendChild(td1);
+        row.appendChild(td2);
+        table.appendChild(row);
+    }
+    div.appendChild(table);
+    div.id = "cache";
+    var current = document.querySelector("#cache");
+    if (current) {
+        document.body.replaceChild(div, current);
+    }
+    else {
+        document.body.appendChild(div);
+    }
+}
+
+function clearCache() {
+    // cheating. Should really send a message to the bg process, but we
+    // skip it for now.
+    window.localStorage.clear();
+}
+
+
+</script>
+<style>
+body {
+    font-family: sans-serif;
+}
+
+h1 {
+    font-size: 26px;
+}
+
+ul label {
+    display: inline-block;
+    max-width: 500px;
+}
+
+ul {
+    list-style: none;
+    padding-left: 0; ;
+    margin: 0;
+}
+
+fieldset {
+    max-width: 500px;
+    margin-bottom: 20px;
+}
+
+li {
+    padding-bottom: 16px;
+}
+
+li > * {
+    vertical-align: top;
+    cursor: pointer;
+}
+
+li:hover > label {
+    background-color: silver;
+}
+
+#about_box {
+    display: none;
+}
+
+div {
+    border: solid 3px black;
+    border-radius: 10px;
+    padding: 15px;
+}
+
+</style>
+<h1>Unshorten options</h1>
+
+<p>
+    Unshorten detects shortened URLs on the page and finds the real URLs
+    in the background. The following three options control how it will
+    treat the links. The example underneath shows how unshorten will treat URLs.
+</p>
+
+
+<ul>
+  <li>
+    <input type="checkbox" id="rewrite_href">
+    <label for="rewrite_href">Change link tags so they go to the unshortened
+    URLs.</label>
+  </li>
+  <li>
+    <input type="checkbox" id="rewrite_textcontent">
+    <label for="rewrite_textcontent">Replace the shortened URLs with the
+    unshortened URLs within the text of link tags.</label>
+  </li>
+  <li>
+    <input type="checkbox" id="add_title">
+    <label for="add_title">Add the unshortened URLs to the title attribute
+    of link tags. The links will be visible when hovering URLS.</label>
+  </li>
+</ul>
+
+
+<fieldset>
+    <legend>URL example:</legend>
+    <a id="preview" href="http://bit.ly/d2Jarx">http://bit.ly/d2Jarx</a>
+</fieldset>
+
+
+<button type="button" id="about_button">About / advanced options</button>
+
+<div id="about_box">
+    Unshorten by <a href="mailto:runefh@gmail.com">Rune Halvorsen</a>.
+    Code and bug tracker available on
+    <a href="http://bitbucket.org/runeh/opera-unshorten">bitbucket</a>.
+    <hr>
+    Supported URL shortener services: <span id="shorteners_list"></span>.
+    File a bug, or submit a patch, to add missing services.
+    <hr>
+    <label for="debug_mode">Enable debug mode. Adds some extra console output
+    and colours links.</label>
+    <input type="checkbox" id="debug_mode">
+    <br>
+    <label for="use_cache">Use cache for unshortened URLs</label>
+    <input type="checkbox" id="use_cache">
+    <br>
+    <button type="button" id="clear_cache">Clear cache of unshortened URLs</button>
+    <button type="button" id="show_cache">Show cache</button>
+
+</div>
Add a comment to this file

extension/yaynay_18.png

Added
New image
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.