Skip to content

Commit 01ed7c9

Browse files
committed
Added Coliru as an optional compilation target. Reworked options page.
1 parent 8a4a3da commit 01ed7c9

File tree

16 files changed

+197
-45
lines changed

16 files changed

+197
-45
lines changed

js/base.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
// dev'ed by SGH. Ideas by LB. Uploading fee provided by BHXSpecter.
2+
function FindParent(item,matchfunc)
3+
{
4+
do {
5+
if(matchfunc(item))
6+
return item;
7+
} while(item = item.parentNode);
8+
}
9+
function GetText(b,c){if(typeof c==="undefined"){return b.textContent||b.innerText}else{if(b.textContent){b.textContent=c}else{b.innerText=c}}}
210
function CloneItem(item) // Clone and item and its childs' callbacks.
311
{ // Used by Code spoilers.
412
var cloned = item.cloneNode(false); // We clone the first node.

js/coliru_run.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/enumerate.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ function RunPageEdit()
8787
forEachItem(AllCodes[i],ItemType,CanReply); // code -> 0, output -> 3
8888
}
8989

90+
var AllCodeBlocks = document.getElementsByClassName('btn trans_half');
91+
for(var i = 0; i != AllCodeBlocks.length; ++i)
92+
{
93+
IsEditablePage = 1;
94+
95+
forEachItem(AllCodeBlocks[i],4,CanReply); // [compile with] box -> 4
96+
}
97+
9098
} while(0);
9199
// The reason behind the do/while(0) is so I can break out of this if the page is not a topic's page.
92100
// This allows me to not having to add multiple nested ifs.

js/main.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
var defaultOptions =
2-
{
3-
allow_quote:'1',
4-
allow_code:'0',
5-
splow_quote:'1',
6-
splow_code:'0',
7-
splow_outp:'1',
8-
spl_qopen:'0',
9-
spl_qsz:'20',
10-
spl_qxsz:'35',
11-
spl_copen:'1',
12-
spl_csz:'20',
13-
spl_popen:'0',
14-
spl_psz:'20'
15-
};
16-
171
chrome.storage.sync.get(defaultOptions,
182
function(items) {
193
for(var k in items)

js/options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function CodeButtonAllowed(){return currentOptions['allow_code'];}
2020
function QuoteSpoilerAllowed(){return currentOptions['splow_quote'];}
2121
function CodeSpoilerAllowed(){return currentOptions['splow_code'];}
2222
function OutputSpoilerAllowed(){return currentOptions['splow_outp'];}
23+
function CompileAllowed(){return currentOptions['allow_cbox'];}
2324

2425
function IsQuoteOpenDefault(){return currentOptions['spl_qopen'];}
2526
function IsCodeOpenDefault(){return currentOptions['spl_copen'];}

js/transform.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,67 @@ function forEachItem(Item, Type, IsReplyable)
8585
newdiv.appendChild(CloneItem(Item));
8686
Item.parentNode.replaceChild(newdiv,Item);
8787
}
88+
if(Type==4 && CompileAllowed())
89+
{
90+
Item.style.height = "32px";
91+
Item.style.whiteSpace = "nowrap";
92+
Item.onmouseout = function(){
93+
Item.style.width = "16px";
94+
Item.style.backgroundColor = "#FFF";
95+
}
96+
Item.onmouseover = function(){
97+
Item.style.width = "100px";
98+
Item.style.backgroundColor = "#FFD";
99+
}
100+
101+
Item.firstChild.firstChild.style.backgroundImage = "url('http://cpp.sh/favicon.ico')";
102+
Item.firstChild.firstChild.style.backgroundSize = "16px";
103+
Item.firstChild.firstChild.className = "C_ico";
104+
105+
var newa = document.createElement('a');
106+
newa.href = '#'; // set onclick
107+
newa.onclick = function()
108+
{
109+
var snippetElem = FindParent(newa,function(elem){
110+
return elem.className == "snippet";
111+
});
112+
if(snippetElem)
113+
{
114+
var srcElem = snippetElem.getElementsByClassName('source');
115+
if(srcElem.length)
116+
{
117+
var code = GetText(srcElem[0]);
118+
window.open('http://coliru.stacked-crooked.com/?src='+escape(code),'_blank');
119+
}
120+
}
121+
// Go upwards from Item, finding the "snippet" classnamed table.
122+
// From the "snippet" classnamed table, find a "source" classnamed table.
123+
// Run dom.text(mysourceclassnamedelement)
124+
// Store it. It's the code we want to compile.
125+
126+
// Open a coliru page as follows:
127+
// window.open('http://coliru.stacked-crooked.com','_blank');
128+
// Execute:
129+
// app.defaultCmd = "ourcode";
130+
// app.resetCommand();
131+
// on it after the page has loaded.
132+
// NOTE: DO NOT USE document.onload ! COLIRU ALREADY USES IT!
133+
// We probably should use
134+
// window.open().document.addEventListener('load',function(){...});
135+
// Inject the code via <script> tags, using the handle window.open() returns.
136+
};
137+
newa.title = "Open Coliru (in a new window)";
138+
newa.target = "_top";
139+
newa.innerHTML = "<span></span> Edit on Coliru";
140+
141+
var newspan = newa.firstChild;
142+
newspan.style.backgroundImage = "url('http://coliru.stacked-crooked.com/favicon.ico')"
143+
newspan.style.backgroundSize = "16px";
144+
newspan.className = "C_ico";
145+
146+
Item.appendChild(document.createElement('br'));
147+
Item.appendChild(newa);
148+
}
88149
}
89150
function forEachPost_Buttons(ButtonStorage, PostID, IsReplyable)
90151
{

manifest.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
"content_scripts" : [{
2323
"matches" : [ "http://www.cplusplus.com/*" ],
2424
"run_at" : "document_end",
25-
"js" : ["js/options.js","js/base.js","js/transform.js","js/enumerate.js","js/main.js"],
25+
"js" : ["options/js/defaults.js","js/options.js","js/base.js","js/transform.js","js/enumerate.js","js/main.js"],
2626
"css" : ["css/style.css"]
27+
},
28+
{
29+
"matches" : [ "http://coliru.stacked-crooked.com/*" ],
30+
"run_at" : "document_end",
31+
"js" : ["js/coliru_run.js"]
2732
}]
2833
}

options/css/custom.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
div#I_optionlist label{margin-left:33%}
2+
div#I_optionlist button{margin-top:5px;width:100%}
3+
div#I_optionlist span{display:block;text-align:center}
4+
.css_status{height:0px;}

options/css/res/bg.png

97.9 KB
Loading

options/css/res/bgh.png

118 Bytes
Loading

0 commit comments

Comments
 (0)