问题
I'm setting up a new chrome extension that suppose to allow/block certain file download. I need to send urls to AWS lambda, process the file and return a boolean back.
I already implemented the chrome extension part that listen to new downloads, pause them and extract the URL. I can't figure out how to send this URL to an AWS lambda service to process. I can't create an AWS object in order to use its services because I use JS on client side and not NodeJS.
import { S3, Lambda } from 'aws-sdk';
var s3 = new S3({apiVersion: '2006-03-01'});
First line throws error:
Uncaught SyntaxError: Unexpected token {
I expected it to compile as the vscode fixed me after I wrote NodeJS syntax:
var AWS = require('aws-sdk');
How can I use AWS services from javascript chrome extension then?
回答1:
This is how you use aws-sdk for chrome extensions
Create an empty new aws.js file.
Copy paste the contents from the https://sdk.amazonaws.com/js/aws-sdk-2.555.0.min.js to the aws.js file we created.
In manifest.json, add it inside the content_scripts.
e.g "js": ["aws.js", "contentScript.js"]
Explanation
Google wants you to provide libraries locally, hence we create the aws.js and a simple copy-paste gets the aws-sdk for js on our local file (it's long)
If you want to access the contents of aws.js in your contentScript.js make sure to add the aws.js inside the same array. If you pass it as a new contentScript object they won't be sharing their content
来源:https://stackoverflow.com/questions/57893562/chrome-extension-which-uses-aws-services