Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a stackoverflow template #63

Merged
merged 8 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions css/templates/overflow.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.code {
background-color: #1c1b1b;
color:white;
padding: 8px;
}
2 changes: 2 additions & 0 deletions js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ document.addEventListener('DOMContentLoaded', () => {
return new YoutubeHover(node, getDomain(CURRENT_TAB));
case 'twitter.com':
return new TwitterHover(node, getDomain(CURRENT_TAB));
case 'stackoverflow.com':
return new StackOverFlowHover(node, getDomain(CURRENT_TAB));
default:
return new BaseHover(node, getDomain(CURRENT_TAB));
//return new BaseHover(node);
Expand Down
1 change: 0 additions & 1 deletion js/templates/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class BaseHover {
.then((res) => {
let parser = new DOMParser();
let doc = parser.parseFromString(res.data, 'text/html');

let title = doc.querySelector('title') ? doc.querySelector('title').innerText : null;
let description = doc.querySelector('meta[name="description"]') ? doc.querySelector('meta[name="description"]').content : null;
let thumbnail = doc.querySelector('meta[name="og:image"]') ? doc.querySelector('meta[name="og:image"]').content : null;
Expand Down
84 changes: 84 additions & 0 deletions js/templates/stackoverflow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* Hover classes bound themselves to a node
*/
class StackOverFlowHover {
constructor(node, CURRENT_TAB) {
this.boundNode = node;
this.redirectLink = node.href;
this.CURRENT_TAB = CURRENT_TAB;
this.linkType = this.checkLinkType();
}

/* Description: This function is unique to every Hover class,
* its goal is to get how many accepted answer are they .
* it can also give the code of the accepted answer.
*/
checkLinkType() {
if (this.CURRENT_TAB != 'stackoverflow.com' && this.redirectLink.includes('/questions/')) {

return 'stackoverflow';
} else {
return 'unknown';
}
}

bindToContainer(node, domain, container) {


if (this.linkType == 'stackoverflow') {
window
.survolBackgroundRequest(node.href,true)
.then((res) => {
let parser = new DOMParser();
let doc = parser.parseFromString(res.data, 'text/html');
let answer_accepted = doc.getElementsByClassName('answer accepted-answer').length

let stackcontainer = document.createElement('div')
stackcontainer.className = "survol-wikipedia-container"

let webTitle = document.createElement('h1');
webTitle.appendChild(document.createTextNode(`${answer_accepted} Answer Accepted !`))

let code_div = document.createElement('div')
code_div.className = "code"

let textContainer = document.createElement('div');
textContainer.className = 'survol-wikipedia-text';

let text = document.createElement('p');
let code = document.createElement('code')

if(answer_accepted == 0){
let code_h = doc.getElementsByClassName('answer')[0].getElementsByTagName('pre')[0]
if(code_h){
let code_t = code_h.getElementsByTagName('code')[0].innerHTML
if(code_t)code.appendChild(document.createTextNode(code_t))
}
text.appendChild(document.createTextNode(doc.getElementsByClassName('s-prose js-post-body')[1].getElementsByTagName('p')[0].innerHTML.slice(0,100)))

}else {
let code_h = doc.getElementsByClassName('answer accepted-answer')[0].getElementsByTagName('pre')[0]
if(code_h){
let code_t = code_h.getElementsByTagName('code')[0].innerHTML
if(code_t)code.appendChild(document.createTextNode(code_t))
}
text.appendChild(document.createTextNode(doc.getElementsByClassName('s-prose js-post-body')[1].getElementsByTagName('p')[0].innerHTML.slice(0,100)))

}
code_div.appendChild(code)

textContainer.appendChild(webTitle);
textContainer.appendChild(text);
textContainer.appendChild(code_div)

stackcontainer.appendChild(textContainer);



container.appendChild(stackcontainer);


})
.catch(console.error);
}
}
}
4 changes: 3 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@
"js/templates/reddit.js",
"js/templates/twitter.js",
"js/templates/youtube.js",
"js/templates/stackoverflow.js",
"js/core.js"
],
"css": [
"css/base.css",
"css/templates/wikipedia.css",
"css/templates/reddit.css",
"css/templates/twitter.css",
"css/templates/youtube.css"
"css/templates/youtube.css",
"css/templates/overflow.css"
],
"run_at": "document_start"
}
Expand Down