cymplecy

S3GPIO chrome ext

Jan 28th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const filters = {
  2.     urls: [
  3.         "*://translate-service.scratch.mit.edu/translate*"
  4.     ]
  5. }
  6.  
  7. chrome.webRequest.onBeforeRequest.addListener(function (details) {
  8.     var url = details.url
  9.     var text = (new URLSearchParams(url)).get("text")
  10.  
  11.     try {
  12.         console.log(text);
  13.         if (text.substring(0,1) == "[") {
  14.             // proxy this request to a url of our choosing
  15.             return {
  16.                 redirectUrl: "https://localhost/translate?language=fr&text=" + text
  17.             }
  18.         }
  19.     } catch (e) {
  20.         // do nothing (ie. continue the request) if no [ as 1st char in text
  21.     }
  22. }, filters, ["blocking"])
  23.  
  24.  
  25. chrome.webRequest.onHeadersReceived.addListener(function(details) {
  26.     console.log(1)
  27.     // add the header
  28.     details.responseHeaders.push({
  29.         name: 'Access-Control-Allow-Origin',
  30.         value:'*'
  31.     })
  32.     console.log(details.responseHeaders)
  33.  
  34.     // return the new headers
  35.     return {
  36.         responseHeaders: details.responseHeaders
  37.     }
  38. }, {urls: ['*://127.0.0.1/*', '*://localhost/*']},['responseHeaders', 'blocking'])
Add Comment
Please, Sign In to add comment