Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

amazon web services - Loading AWS sdk in chrome extension

In my chrome extension, I was trying to load the AWS Cognito js SDK in the popup type iframe HTML where the HTML is bundled by react, now the problem is I am not able to access AWS instance or ( window.AWS ) in my useEffect to perform user authentication

PS: I tried to get the window object too like below but it was hard luck.

chrome.tabs.query(
      {active: true, windowType: 'normal', currentWindow: true},
      function (d) {
        chrome.tabs.get(d[0].id, function (tab) {
          chrome.windows.get(tab.windowId, function (win) {
            console.log('App -> win', win)
            debugger // THIS IS THE WINDOW OBJECT
          })
        })
      },
    )

from background_script.js I get a trigger for on click extension icon

function gotMessage(payload) {
  if (payload.message === 'SIGNAL_TAB') {
    iframe.src = chrome.extension.getURL('./index.html')
    document.body.appendChild(iframe)

    toggle()
  }
}

In Index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <script src="https://sdk.amazonaws.com/js/aws-sdk-2.831.0.min.js"></script>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
  </body>
</html>

In my App.js

function App() { 
useEffect(() => {
    console.log('App -> window.AWS', window)
    chrome.tabs.query(
      {active: true, windowType: 'normal', currentWindow: true},
      function (d) {
      console.log("App -> d", d)
        chrome.tabs.get(d[0].id, function (tab) {
          chrome.windows.get(tab.windowId, function (win) {
            console.log('App -> win', win)
          })
        })
      },
    )
    return <SomeJSX />

}

Here is my index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);

Manifest.json

{
  "manifest_version": 2,
  "name": "Elixir AI",
  "description": "Elixir AI description",
  "version": "1.0",
  "permissions": ["tabs", "bookmarks", "unlimitedStorage", "storage",  "http://*/", "https://*/"],
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["./content.js"]
    }
  ],
  "web_accessible_resources": ["index.html"],
  "background": {
    "scripts": ["./background.js"]
  },
  "browser_action": {
    "default_icon": "elixir.png"
  },
  "content_security_policy": "script-src 'self' 'sha256-Vkb/pSN52JgiWSIfh1wav6XGzM3ULeZ5d9VaHeA5JQ8='; object-src 'self'"
}

Background.js

chrome.browserAction.onClicked.addListener(buttonClicked)
chrome.tabs.query({active: true, lastFocusedWindow: true}, (tabs) => {
  let url = tabs[0].url
  new Promise((res, rej) => {
    window.linkedin_url = url
    localStorage.setItem('linkedin_url', url)
    res(url)
  })
})

function buttonClicked(tab) {
  var payload = {
    id: tab.id,
    tabs: chrome.tabs,
    eventTab: tab,
    message: "SIGNAL_TAB",
    linkedin_url: tab.url
  }
  chrome.tabs.sendMessage(tab.id, payload)
}
question from:https://stackoverflow.com/questions/65897727/loading-aws-sdk-in-chrome-extension

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...