Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Settings/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@ window.onload = function(){
updateSnippetHistory(snipArray[i]);
}
})
}
}
8 changes: 6 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"homepage_url": "https://github.com/studenton/cutcode",
"options_ui": {

"page": "Settings/UserSettings.html",
"chrome_style": true
},
Expand All @@ -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
}
}
64 changes: 58 additions & 6 deletions scripts/stackoverflow/inject.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()+'<br/>';
}
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();
Expand All @@ -51,12 +100,15 @@ 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);
} catch (err) {
console.log('Failed to copy', err);
}
});
});
});