0

manifest.json

{
    "manifest_version": 2,
    "name": "F",
    "version": "1.0",    
    "icons": {
            "16": "images/empty_16.png",
            "32": "images/empty_32.png",
            "48": "images/empty_48.png",
            "64": "images/empty_64.png",
            "128": "images/empty_128.png"
    },

    "content_scripts": [
        {
            "matches": [ "https://*/","http://*/"],
            "js": [ "scripts/extract.js"],
            "run_at": "document_end"
        }
    ],
    "permissions": ["http://*/","https://*/"],
    "browser_action": {
          "default_icon": {
            "16": "images/empty_16.png",
            "32": "images/empty_32.png",
            "48": "images/empty_48.png",
            "64": "images/empty_64.png",
            "128": "images/empty_128.png"
          },
          "default_title": "F",
          "default_popup": "popup/popup.html"
    }
}

popup.html

<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <script src="popup.js"></script>
  <title></title>
  </head>
  <body>
    <button id="get_ids">Get IDs</button>
    <button id="get_ans">Get Ans</button>
    <button id="send_ans">Send Ans</button>
  </body>
</html>

popup.js

var wait={};
document.addEventListener('DOMContentLoaded', function () {
    document.getElementById('get_ids').addEventListener('click', getIDS());
    document.getElementById('get_ans').addEventListener('click', getAns(wait));
    document.getElementById('send_ans').addEventListener('click', sendAns());
})

function getIDS(){
    // on first page, with 'user' rights
    //some POST requests with fetch, need to be run on web page context, just like from console
    //JSON answer from server needs to be saved somwhere, example answer: wait = {"tasks_count":2,"tasks":[{"id":00001,"num":1},{"id":00002,"num":2}]};
}

function getAns(wait){
    // on second page, with 'admin' rights
    //some fetches to server while not all IDs from wait are in quests.
    //should save somewhere object with answers named quests.
}

function sendAns(){
    // on first page
    //should send some fetches based on current page location
    //fetch example: fetch("<https address>/rest/secure/api/answer/<variant id>/<question id>", {"headers": {<some headers>},
    //"body": "{\"answer\":{\"id\":\"<id>\",\"@answer_type\":\"answer/single\"}}",
    //"method": "POST",
    //"mode": "cors",
    //"credentials": "include"});
    // answer types are different, can be extracted from json from second page (getAns function).
}

extract.js is empty

Expected work: Press Get IDs button on 'user' page, wait object should be updated with fetched data. Then on 'admin' page press Get Ans, quest object should now contain needed questions (with ids from wait object), questions is array of objects {id:XXX,question:"",content:[],answers_hr:{},answers_send:{}} (answers_hr is human readable, answers_send is what to send via sendAns()) Then on 'user' page press Send Ans, function sendAns should fetch() to server for each question from wait, excluding ones with type "answer/free". When this type appears script should stop, warn user to type answer in input, send it, then continue.

Reality: Scripts are not executed. Will objects be saved for different pages? If not then how to transfer them?

Anonymix321
  • 107
  • 6
  • 1) Remove `()` after all your three functions in addEventListener, 2) Use a content script to access the tab, [info](https://stackoverflow.com/a/4532567), 3) To access the page context use a script element in the content script, [info](https://stackoverflow.com/a/9517879). – wOxxOm Apr 15 '20 at 14:41
  • How to transfer object state from one page to another? – Anonymix321 Apr 15 '20 at 21:38
  • Depends. You can use [messaging](https://developer.chrome.com/extensions/messaging) or chrome.storage.local. – wOxxOm Apr 16 '20 at 02:51
  • Which of them will work with Incognito Tabs? (One instance of extension works in normal mode, other - in Incognito. Is this the only way to fetch with different user accounts?) – Anonymix321 Apr 18 '20 at 13:53

0 Answers0