File size: 1,529 Bytes
c942159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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));
}