Rune Halvorsen avatar Rune Halvorsen committed b154539

Initial commit. Semi-working detection and adding

Comments (0)

Files changed (4)

extension/config.xml

+<?xml version="1.0" encoding="utf-8"?>
+<widget xmlns="http://www.w3.org/ns/widgets">
+  <name>Transmit</name>
+  <description>
+    Send torrent links to the transmission bittorrent client.
+  </description>
+  <author href="http://bitbucket.org/runeh/" email="runefh@gmail.com">
+    Rune Halvorsen
+  </author>
+
+  <access origin="*"/>
+</widget>

extension/includes/transmit.js

+window.addEventListener('DOMContentLoaded', function() {
+    opera.postError("transmit!");
+    opera.extension.onmessage = onMessage;
+    var options = {};
+    var resolved = {};
+
+    //    document.body.addEventListener('DOMNodeInserted', onNodeInserted, false);
+    init();
+
+    function init() {
+        var elems = findTorrentLinks();
+        opera.postError("found links: " + elems.length);
+        elems.forEach(function(e) { e.addEventListener("click", onLinkActivated, false) });
+    }
+
+    function onLinkActivated(evt) {
+        evt.preventDefault();
+        // fixme check opts
+        opera.extension.postMessage(encodeMessage("launch_torrent", evt.srcElement.href));
+    }
+
+    function onMessage(event) {
+        var message = decodeMessage(event.data);
+        switch (message.type) {
+            case "options":
+                options = message.data;
+            break;
+        }
+    }
+
+    function onNodeInserted(event) {
+        unshorten(event.target);
+    }
+
+    function decodeMessage(data) {
+        return JSON.parse(data);
+    }
+
+    function encodeMessage(messagetype, data) {
+        return JSON.stringify({type: messagetype, data: data});
+    }
+
+    function findTorrentLinks(node) {
+        node = node && node.querySelectorAll ? node : document;
+        var elems = Array.prototype.slice.call(node.querySelectorAll("a"), 0);
+        elems = elems.filter(function(e) {
+            return e.href.indexOf(".torrent") != -1 ||
+                e.href.indexOf("magnet:") == 0
+        });
+        return elems;
+    }
+}, false);

extension/index.html

+<!doctype html>
+<script>
+window.addEventListener("load", setupConnection, false);
+
+function s2b(s) {
+    return s=="true" ? true : false;
+}
+
+function setupConnection()
+{
+    opera.extension.onconnect = onConnectHandler;
+    opera.extension.onmessage = onMessageHandler;
+}
+
+function transmissionCommand(url, method, args, optargs) {
+    var conn = new XMLHttpRequest();
+    conn.open("POST", url, false);
+    conn.setRequestHeader("Content-Type", "application/json");
+    if (window.sid) {
+        conn.setRequestHeader("X-Transmission-Session-Id", window.sid);
+    }
+    conn.send(JSON.stringify({method: method, "arguments": args}));
+    if (conn.status == 409) {
+        window.sid = conn.getResponseHeader("X-Transmission-Session-Id");
+        transmissionCommand(url, method, args, optargs);
+    }
+    else {
+        opera.postError(conn.status);
+        opera.postError(conn.responseText);
+    }
+    
+}
+              
+function onConnectHandler(event) {
+//    sendOptions(event.source);
+}
+
+function onMessageHandler(event) {
+    message = decodeMessage(event.data);
+    switch (message.type) {
+        case "launch_torrent":
+            launchTorrent(message.data);
+        break;
+    }
+}
+
+function launchTorrent(url) {
+    opera.postError("got launch " + url);
+    var api = "http://gregor:9091/transmission/rpc";
+    var method = "torrent-add";
+    var args = {filename:url};
+    var optargs = null;
+    transmissionCommand(api, method, args, optargs);    
+}
+
+function sendOptions(destination) {
+    destination.postMessage(
+        encodeMessage("options",
+                      {
+
+                      }
+                     )
+    );
+}
+
+function encodeMessage(messagetype, data) {
+    return JSON.stringify({type: messagetype, data: data});
+}
+
+function decodeMessage(data) {
+    return JSON.parse(data);
+}
+
+</script>

extension/options.html

+<!doctype html>
+<script src="includes/unshortinator.js"></script>
+<script>
+
+window.onload = function() {
+//    setOptions();
+  //  document.querySelector("#clear_cache").addEventListener("click", clearCache, false);
+}
+
+function s2b(s) {
+    return s=="true" ? true : false;
+}
+
+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,
+    };
+}
+
+
+</script>
+<style>
+body {
+    font-family: sans-serif;
+}
+
+h1 {
+    font-size: 26px;
+}
+
+#about_box {
+    display: none;
+}
+
+div {
+    border: solid 3px black;
+    border-radius: 10px;
+    padding: 15px;
+}
+
+</style>
+<h1>Transmit options</h1>
+
+<p>
+  copytext
+</p>
+
+<label for="api_url">API url</label><input id="api_url">
+<button type="button">save</button>
+<button type="button">test</button>
+<br>
+<br>
+<br>
+<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>.
+</div>
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.