Skip to content

Instantly share code, notes, and snippets.

@qavan
Created May 9, 2020 13:49
Show Gist options
  • Save qavan/2ed650789988d9ee185ea06b25dc3517 to your computer and use it in GitHub Desktop.
Save qavan/2ed650789988d9ee185ea06b25dc3517 to your computer and use it in GitHub Desktop.
chrome.runtime.onMessage.addListener(
async data => {
let textWhatIGot = data.text;
if (data.action === "prod") {
const response = await fetch("https://1c.codepool.ru/api/info", {
method: "POST",
headers: {
"Content-Type": "application/json;charset=utf-8",
},
body: JSON.stringify({
query: textWhatIGot,
}),
});
const result = await response.json();
console.log(result);
let answers = [];
for (let i = 0; i < result.data.length; i++) {
let candidate = result.data[i].item;
if (candidate.text === textWhatIGot) {
if (candidate.type === "SINGLE") {
for (let j = 0; j < candidate.variants.length; j++) {
try {
if (candidate.variants[j].correct) {
answers.push(candidate.variants[j].text.toString());
break;
}
}
catch (e) {
console.log(e)
}
}
}
}
}
console.log(answers);
if (answers.length === 1) {
chrome.notifications.create(
'',
{
title: 'Ответ',
message: answers[0],
contextMessage: answers[0],
iconUrl: '/images/get_started32.png',
type: 'basic'
},
function () {
console.log("Notification showed")
});
}
else {
chrome.notifications.create(
'',
{
title: 'Ответ',
message: 'Найдено множество ответов, поищите на сайте либо выделите область побольше',
contextMessage: 'Найдено множество ответов, поищите на сайте либо выделите область побольше',
iconUrl: '/images/get_started32.png',
type: 'basic'
},
function () {
console.log("Notification showed")
});
}
return true;
}
else if (data.action === "timeout") {
chrome.notifications.create('', {
title: textWhatIGot,
message: textWhatIGot,
contextMessage: textWhatIGot,
iconUrl: '/images/get_started32.png',
type: 'basic'
},function () {
console.log("Suka, stop spamming, timeout is X seconds");
});
}
});
{
"name": "WINTERZZ API",
"version": "0.0.0.1",
"description": "WINTERZZ SIMPLIFICATOR",
"permissions": [
"http://*/*",
"https://*/*",
"notifications"
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["script.js"]
}
],
"icons": {
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"128": "images/get_started128.png"
},
"manifest_version": 2
}
let timeout = 4000;
let timeOfStart = Date.now();
console.log("Started at " + timeOfStart);
document.addEventListener('mouseup',async function(event) {
let candidateTime = Date.now();
console.log("CandidateTime at " + candidateTime);
let selectedText = window.getSelection().toString();
if (selectedText.length <= 20) return;
if (timeOfStart + timeout <= candidateTime) {
timeOfStart = candidateTime;
console.log(selectedText);
chrome.runtime.sendMessage('', {
action: "prod",
text: selectedText
});
}
else {
timeOfStart = candidateTime;
chrome.runtime.sendMessage('', {
action: "timeout",
text: "Подождите 4 секунды!"
});
}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment