vera / chrome-ext /background.js
liujch1998's picture
Chrome extension
c942159
raw
history blame
No virus
1.53 kB
chrome.action.onClicked.addListener((tab) => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: getHighlightedText,
}, (result) => {
if (chrome.runtime.lastError) {
console.error(chrome.runtime.lastError.message);
return;
}
if (result.length == 0) {
console.error('No result returned by getHighlightedText!');
return;
}
let text = result[0].result;
if (text.length > 0) {
getPlausibility(text, tab);
}
});
})
// this function is executed in the context of the current tab
function getHighlightedText() {
let text = window.getSelection().toString();
console.log(`Highlighted text: ${text}`);
if (text.length == 0) {
alert('Please highlight some text first!');
}
return text;
}
// this function is executed in the context of the extension background
function getPlausibility(text, tab) {
const URL = 'https://qa.cs.washington.edu:8372';
const data = { statement: text };
fetch(URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(response => response.json())
.then(output => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: (statement, score) => {
disp_score = Math.round(score * 100);
alert(`Statement: ${statement}\nPlausibility: ${disp_score}%`);
},
args: [output.statement, output.score_calibrated],
});
})
.catch((error) => console.error('Error:', error));
}