Johannes Zellner пре 11 година
родитељ
комит
c6014f7975

+ 9 - 0
chrome_extension/detectapp.js

@@ -0,0 +1,9 @@
+'use strict';
+
+var settingsNode = document.getElementById('guacamoly-settings-node');
+if (settingsNode) {
+    try {
+        // implicit return of object
+        JSON.parse(settingsNode.textContent);
+    } catch (e) {}
+}

BIN
chrome_extension/logo.png


+ 13 - 0
chrome_extension/manifest.json

@@ -0,0 +1,13 @@
+{
+  "manifest_version": 2,
+  "name": "Guacamoly",
+  "description": "This extension allows to add the current page as a Guacamoly thing.",
+  "version": "1.0",
+  "browser_action": {
+    "default_icon": "logo.png",
+    "default_popup": "popup.html"
+  },
+  "permissions": [
+    "activeTab"
+  ]
+}

+ 46 - 0
chrome_extension/popup.css

@@ -0,0 +1,46 @@
+body {
+    font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
+    font-size: 100%;
+    width: 450px;
+    background: #eee;
+}
+
+textarea {
+    width: 100%;
+    height: 80px;
+    max-width: 100%;
+    padding: 0;
+    border: 0;
+}
+
+.hide {
+    display: none;
+}
+
+button {
+    width: 100%;
+}
+
+.spinner {
+    background-color: transparent;
+    border: 3px solid #03a9f4;
+    border-radius: 50%;
+    border-top-color: transparent;
+    border-right-color: transparent;
+    width: 1em;
+    height: 1em;
+    display: inline-block;
+    vertical-align: middle;
+    box-sizing: border-box;
+    animation: spinner-animation 0.65s infinite linear;
+    font-size: 35px;
+}
+
+@keyframes spinner-animation {
+    0% {
+        transform:rotate(0deg)
+    }
+    100% {
+        transform:rotate(360deg)
+    }
+}

+ 33 - 0
chrome_extension/popup.html

@@ -0,0 +1,33 @@
+
+<!doctype html>
+<!--
+ This page is shown when the extension button is clicked, because the
+ "browser_action" field in manifest.json contains the "default_popup" key with
+ value "popup.html".
+ -->
+<html>
+  <head>
+    <title>Guacamoly</title>
+
+    <link rel="stylesheet" href="popup.css">
+    <script src="popup.js"></script>
+
+  </head>
+  <body>
+    <div id="main" class="hide">
+      <textarea id="content"></textarea>
+      <br/>
+      <button id="actionAdd">Add</button>
+    </div>
+    <div id="setup" class="hide">
+      <p>Please visit your Guacamoly app</p>
+    </div>
+    <div id="done" class="hide">
+      <p>Successfully added</p>
+    </div>
+    <div id="busy" class="hide">
+      <center><i class="spinner"></i></center>
+    </div>
+  </body>
+</html>
+

+ 81 - 0
chrome_extension/popup.js

@@ -0,0 +1,81 @@
+'use strict';
+
+// Query filter to be passed to chrome.tabs.query - see
+// https://developer.chrome.com/extensions/tabs#method-query
+var queryInfo = {
+    active: true,
+    currentWindow: true
+};
+
+// localStorage.settingsUrl = 'https://things.nebulon.info/api/things?token=70c40f76-75cb-49a0-bf97-e06815208baf';
+
+function getCurrentTabUrl(callback) {
+    chrome.tabs.query(queryInfo, function(tabs) {
+        callback(tabs[0].url);
+    });
+}
+
+function tryToDetectSettings() {
+    chrome.tabs.query(queryInfo, function(tabs) {
+        chrome.tabs.executeScript(tabs[0].id, { file: 'detectapp.js' }, function (results) {
+            if (results[0] && results[0].addPostUrl) {
+                localStorage.settingsUrl = results[0].addPostUrl;
+                show('main');
+            }
+        });
+    });
+}
+
+document.addEventListener('DOMContentLoaded', function() {
+    if (!localStorage.settingsUrl) {
+        show('setup');
+        tryToDetectSettings();
+    } else {
+        show('main');
+    }
+
+    document.getElementById('actionAdd').addEventListener('click', add);
+
+    getCurrentTabUrl(function(url) {
+        document.getElementById('content').value = url;
+    });
+});
+
+var views = [
+    'main',
+    'done',
+    'setup',
+    'busy'
+];
+
+function show(view) {
+    views.forEach(function (view) {
+        document.getElementById(view).classList.add('hide');
+    });
+
+    document.getElementById(view).classList.remove('hide');
+}
+
+function add() {
+    show('busy');
+
+    var http = new XMLHttpRequest();
+    var body = JSON.stringify({ content: document.getElementById('content').value });
+    http.open('POST', localStorage.settingsUrl, true);
+
+    http.setRequestHeader('Content-type', 'application/json');
+    http.setRequestHeader('Content-length', body.length);
+    http.setRequestHeader('Connection', 'close');
+
+    http.onreadystatechange = function() {//Call a function when the state changes.
+        if(http.readyState === 4) {
+            if (http.status === 401) {
+                delete localStorage.settingsUrl;
+                show('setup');
+                return;
+            }
+            if (http.status === 201) return show('done');
+        }
+    };
+    http.send(body);
+}