From 8084e09f9f8441305553fbca6edec79c743e049a Mon Sep 17 00:00:00 2001 From: jthompson Date: Thu, 16 Mar 2017 16:37:00 -0500 Subject: [PATCH 1/3] added settings, support for settings in inject.js --- Settings/UserSettings.html | 21 ++++++++++++++ Settings/settings.js | 32 +++++++++++++++++++++ manifest.json | 14 ++++++--- scripts/stackoverflow/inject.js | 50 +++++++++++++++++++++++++++++++-- 4 files changed, 110 insertions(+), 7 deletions(-) create mode 100644 Settings/UserSettings.html create mode 100644 Settings/settings.js diff --git a/Settings/UserSettings.html b/Settings/UserSettings.html new file mode 100644 index 0000000..50dae7d --- /dev/null +++ b/Settings/UserSettings.html @@ -0,0 +1,21 @@ + + + + + + + + + +
+ + + + +
+ +
+ + + + \ No newline at end of file diff --git a/Settings/settings.js b/Settings/settings.js new file mode 100644 index 0000000..736fc1f --- /dev/null +++ b/Settings/settings.js @@ -0,0 +1,32 @@ +//referenced formatting from @chenhunghan: https://github.com/HIIT/dime-webextension/blob/master/src/webextension/options.js#L15 + +function saveOptions(e) { + e.preventDefault(); + + + console.log("saving options"); + + browser.storage.local.set({ + color: document.getElementById('color').value, + commenting: document.getElementById('commenting').value, + attribution: document.getElementById('attribution').checked + }); + document.getElementById('status').innerHTML = 'test'; + + setTimeout(function(){ + document.getElementById('status').innerHTML = ''; + }, 1000); +} + +function restoreOptions() { + console.log("page load"); + browser.storage.local.get(['color', 'attribution', 'commenting'], (items) => { + const{color, attribution, commenting} = items; + document.getElementById('color').value = color; + document.getElementById('attribution').value = attribution; + document.getElementById('commenting').value = commenting; + }); +} + +document.addEventListener("DOMContentLoaded", restoreOptions); +document.querySelector("form").addEventListener("submit", saveOptions); \ No newline at end of file diff --git a/manifest.json b/manifest.json index a9ce048..aa55c27 100644 --- a/manifest.json +++ b/manifest.json @@ -1,20 +1,26 @@ { "name": "Cutcode", "version": "0.1.0", + "manifest_version": 2, "description": "Double click to copy code from Stack Overflow.", "author": "Amit Chaudhary ", "homepage_url": "https://github.com/studenton/cutcode", - "manifest_version": 2, + "options_ui": { + "page": "settings/UserSettings.html" + }, "permissions": [ - "clipboardWrite" + "clipboardWrite", + "storage" ], "content_scripts": [{ "matches": ["*://*.stackoverflow.com/*", "*://*.stackexchange.com/*", "*://github.com/*"], - "js": ["scripts/stackoverflow/inject.js"], + "js": ["scripts/stackoverflow/inject.js", + "scripts/settings/settings.js" + ], "run_at": "document_end" }], "icons": { "144": "images/icon.png" }, "offline_enabled": true -} +} \ No newline at end of file diff --git a/scripts/stackoverflow/inject.js b/scripts/stackoverflow/inject.js index 6278466..3ad7818 100644 --- a/scripts/stackoverflow/inject.js +++ b/scripts/stackoverflow/inject.js @@ -1,21 +1,65 @@ 'use strict'; +var color = '', +attribution = false, +commenting = '', +codeAttribution = ''; + +function onError(error) { + console.log(`Error: ${error}`); +} + +function onGot(item) { + console.log(item.commenting); + if (item.color){ //is color set + color = item.color; + } + else{ + color = '#0D0'; + } + if (item.attribution){ //is attribution checked + attribution = true; + if (item.commenting){ //is commenting set + commenting = item.commenting; + } + } +} + +var getting = browser.storage.local.get(); +getting.then(onGot, onError); Array.from(document.getElementsByTagName('pre')) // get all code snippets .forEach(function (block) { block.addEventListener('dblclick', function (event) { // Reference: http://stackoverflow.com/a/6462980/3485241 + console.log(commenting); + //gets the address of the current page + if (attribution){ + codeAttribution = '

' + commenting + ' source: ' + window.location.toString(); + } + var copyText = block.innerHTML; + + copyText = copyText + codeAttribution; + //copy old HTML + var oldHTML = block.innerHTML; + + //replace with new HTML + block.innerHTML = copyText; // Add snippet to range var range = document.createRange(); range.selectNode(block); - + + // Copy snippet to clipboard try { window.getSelection().addRange(range); document.execCommand('copy'); window.getSelection().removeAllRanges(); - block.style.border = '2px solid #0D0'; + + //replace new with old HTML + block.innerHTML = oldHTML; + block.style.border = '2px solid '+color; setTimeout(function () { return block.style.border = 'none'; }, 500); @@ -23,4 +67,4 @@ Array.from(document.getElementsByTagName('pre')) // get all code snippets console.log('Failed to copy', err); } }); -}); +}); \ No newline at end of file From 14bb949c5c85c8de4880dae1733f20c2fe5f215a Mon Sep 17 00:00:00 2001 From: jthompson Date: Tue, 21 Mar 2017 11:54:04 -0500 Subject: [PATCH 2/3] added browser detect, fixed settings ref, added restored options --- Settings/UserSettings.html | 2 +- Settings/settings.js | 38 ++++++++++++++++++++++++--------- manifest.json | 2 +- scripts/stackoverflow/inject.js | 25 ++++++++++++++++------ 4 files changed, 48 insertions(+), 19 deletions(-) diff --git a/Settings/UserSettings.html b/Settings/UserSettings.html index 50dae7d..50afa50 100644 --- a/Settings/UserSettings.html +++ b/Settings/UserSettings.html @@ -9,7 +9,7 @@
- +
diff --git a/Settings/settings.js b/Settings/settings.js index 736fc1f..3dbf1d2 100644 --- a/Settings/settings.js +++ b/Settings/settings.js @@ -3,15 +3,18 @@ function saveOptions(e) { e.preventDefault(); - - console.log("saving options"); + +if (isNotChrome){ + browser = chrome; +} + browser.storage.local.set({ color: document.getElementById('color').value, commenting: document.getElementById('commenting').value, attribution: document.getElementById('attribution').checked }); - document.getElementById('status').innerHTML = 'test'; + document.getElementById('status').innerHTML = 'Saved!'; setTimeout(function(){ document.getElementById('status').innerHTML = ''; @@ -19,14 +22,29 @@ function saveOptions(e) { } function restoreOptions() { - console.log("page load"); - browser.storage.local.get(['color', 'attribution', 'commenting'], (items) => { - const{color, attribution, commenting} = items; - document.getElementById('color').value = color; - document.getElementById('attribution').value = attribution; - document.getElementById('commenting').value = commenting; - }); + if (isNotChrome){ + chrome.storage.local.get(null, function(item){ + document.getElementById('color').value = item.color || '#0D0'; + document.getElementById('attribution').checked = item.attribution || false; + document.getElementById('commenting').value = item.commenting || ''; + }); + }//handle firefox + else{ + browser.storage.local.get(['color', 'attribution', 'commenting'], (items) => { + const{color, attribution, commenting, hotkey} = items; + document.getElementById('color').value = color ||'#0D0'; + document.getElementById('attribution').checked = attribution || false; + document.getElementById('commenting').value = commenting || ''; + }); + + }; +} + +//checks if browser is defined. if not, current browser is probably Chrome or other. +function isNotChrome() { + return typeof browser !== typeof undefined ? true : false; } + document.addEventListener("DOMContentLoaded", restoreOptions); document.querySelector("form").addEventListener("submit", saveOptions); \ No newline at end of file diff --git a/manifest.json b/manifest.json index aa55c27..890aa93 100644 --- a/manifest.json +++ b/manifest.json @@ -15,7 +15,7 @@ "content_scripts": [{ "matches": ["*://*.stackoverflow.com/*", "*://*.stackexchange.com/*", "*://github.com/*"], "js": ["scripts/stackoverflow/inject.js", - "scripts/settings/settings.js" + "settings/settings.js" ], "run_at": "document_end" }], diff --git a/scripts/stackoverflow/inject.js b/scripts/stackoverflow/inject.js index 3ad7818..391b466 100644 --- a/scripts/stackoverflow/inject.js +++ b/scripts/stackoverflow/inject.js @@ -2,14 +2,14 @@ var color = '', attribution = false, commenting = '', -codeAttribution = ''; +codeAttribution = '', +hotkey = ''; function onError(error) { console.log(`Error: ${error}`); } function onGot(item) { - console.log(item.commenting); if (item.color){ //is color set color = item.color; } @@ -23,23 +23,34 @@ function onGot(item) { } } } +function isNotChrome(){ + return typeof browser !== typeof undefined ? true : false; +} + + + +if (isNotChrome){ + chrome.storage.local.get(null, onGot); +} +else{ + var getting = browser.storage.local.get(); + getting.then(onGot, onError); +} + -var getting = browser.storage.local.get(); -getting.then(onGot, onError); Array.from(document.getElementsByTagName('pre')) // get all code snippets .forEach(function (block) { - block.addEventListener('dblclick', function (event) { // Reference: http://stackoverflow.com/a/6462980/3485241 console.log(commenting); //gets the address of the current page if (attribution){ - codeAttribution = '

' + commenting + ' source: ' + window.location.toString(); + codeAttribution = commenting + ' source: ' + window.location.toString()+'
'; } var copyText = block.innerHTML; - copyText = copyText + codeAttribution; + copyText = codeAttribution+ copyText; //copy old HTML var oldHTML = block.innerHTML; From b826e544121070dd8513f5415cc002b834b33c87 Mon Sep 17 00:00:00 2001 From: jthompson Date: Wed, 22 Mar 2017 15:51:39 -0500 Subject: [PATCH 3/3] Updated usersettings.htmlto match master --- Settings/UserSettings.html | 49 +++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/Settings/UserSettings.html b/Settings/UserSettings.html index 50afa50..9a2a9ca 100644 --- a/Settings/UserSettings.html +++ b/Settings/UserSettings.html @@ -1,21 +1,38 @@ - - - - - + + + + + -
- - - - -
- -
- - +
+ + + + + + + + + + + + + + + + + + + + +
+
- \ No newline at end of file +
+ + + +