diff --git a/Settings/settings.js b/Settings/settings.js index e32eee3..6517cca 100644 --- a/Settings/settings.js +++ b/Settings/settings.js @@ -96,4 +96,4 @@ window.onload = function(){ updateSnippetHistory(snipArray[i]); } }) -} +} \ No newline at end of file diff --git a/manifest.json b/manifest.json index e92c471..69611eb 100644 --- a/manifest.json +++ b/manifest.json @@ -1,10 +1,12 @@ { "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", "options_ui": { + "page": "Settings/UserSettings.html", "chrome_style": true }, @@ -15,11 +17,13 @@ ], "content_scripts": [{ "matches": ["*://*.stackoverflow.com/*", "*://*.stackexchange.com/*", "*://github.com/*"], - "js": ["scripts/stackoverflow/inject.js"], + "js": ["scripts/stackoverflow/inject.js", + "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 966e15d..0e58d5d 100644 --- a/scripts/stackoverflow/inject.js +++ b/scripts/stackoverflow/inject.js @@ -1,4 +1,43 @@ 'use strict'; +var color = '', +attribution = false, +commenting = '', +codeAttribution = '', +hotkey = ''; + +function onError(error) { + console.log(`Error: ${error}`); +} + +function onGot(item) { + 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; + } + } +} +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); +} + + //Executed when the extension is invoked. This will only do things //the first time the extension is loaded up. It sets up @@ -18,15 +57,25 @@ chrome.storage.local.get(null, function(result){ 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 = codeAttribution+ copyText; + //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().removeAllRanges(); @@ -51,7 +100,10 @@ Array.from(document.getElementsByTagName('pre')) // get all code snippets }); window.getSelection().removeAllRanges(); - block.style.outline = '2px solid #0D0'; + + //replace new with old HTML + block.innerHTML = oldHTML; + block.style.border = '2px solid '+color; setTimeout(function () { return block.style.outline = 'none'; }, 500); @@ -59,4 +111,4 @@ Array.from(document.getElementsByTagName('pre')) // get all code snippets console.log('Failed to copy', err); } }); -}); +}); \ No newline at end of file