Skip to content

Instantly share code, notes, and snippets.

@rynomad
Created October 7, 2024 00:32
Show Gist options
  • Save rynomad/0dbdc07217d3450252ee3f20ed3ccf54 to your computer and use it in GitHub Desktop.
Save rynomad/0dbdc07217d3450252ee3f20ed3ccf54 to your computer and use it in GitHub Desktop.
build rettiwt-api for chrome extension
#!/bin/bash
# Create project structure
mkdir -p rettiwt-extension/src/shims
mkdir -p rettiwt-extension/dist
cd rettiwt-extension
# Initialize npm project and install dependencies
npm init -y
npm install webpack webpack-cli babel-loader @babel/core @babel/preset-env \
buffer stream-browserify crypto-browserify querystring-es3 \
process rettiwt-api
# Create webpack.config.js
cat << EOF > webpack.config.js
const path = require('path');
const webpack = require('webpack');
module.exports = {
mode: 'production',
entry: './src/content.js',
output: {
filename: 'content.js',
path: path.resolve(__dirname, 'dist'),
},
resolve: {
fallback: {
https: false,
http: false,
url: false,
util: false,
buffer: require.resolve('buffer/'),
stream: require.resolve('stream-browserify'),
querystring: require.resolve('querystring-es3'),
fs: false,
path: false
},
alias: {
'https': path.resolve(__dirname, 'src/shims/https-shim.js'),
'https-proxy-agent': path.resolve(__dirname, 'src/shims/https-proxy-agent-shim.js')
}
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
]
},
plugins: [
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
process: 'process/browser',
}),
new webpack.NormalModuleReplacementPlugin(
/node:crypto/,
require.resolve('crypto-browserify')
)
]
};
EOF
# Create https-shim.js
cat << EOF > src/shims/https-shim.js
export class Agent {
constructor(options = {}) {
this.options = options;
}
}
export default {
Agent: Agent
};
EOF
# Create https-proxy-agent-shim.js
cat << EOF > src/shims/https-proxy-agent-shim.js
export class HttpsProxyAgent {
constructor(proxyUrl) {
this.proxyUrl = proxyUrl;
}
}
EOF
# Create content.js
cat << EOF > src/content.js
import { Rettiwt } from 'rettiwt-api';
// Creating a new Rettiwt instance
// Note that for accessing user details, 'guest' authentication can be used
const rettiwt = new Rettiwt();
// Fetching the details of the tweet
rettiwt.tweet.details('1842891798441500748')
.then(details => {
console.log("got username details in content script",details);
})
.catch(error => {
console.log("error in content script",error);
});
EOF
# Create manifest.json
cat << EOF > manifest.json
{
"manifest_version": 3,
"name": "Rettiwt Extension",
"version": "1.0",
"description": "A Chrome extension using rettiwt-api",
"permissions": [
"activeTab"
],
"host_permissions": [
"https://x.com/*",
"https://twitter.com/*"
],
"content_scripts": [
{
"matches": ["https://x.com/*", "https://twitter.com/*"],
"js": ["dist/content.js"]
}
]
}
EOF
# Update package.json scripts
npm pkg set scripts.build="webpack"
# Build the extension
npm run build
echo "Chrome extension project has been set up in the 'rettiwt-extension' directory."
echo "To load the extension in Chrome:"
echo "1. Go to chrome://extensions/"
echo "2. Enable 'Developer mode'"
echo "3. Click 'Load unpacked'"
echo "4. Select the 'rettiwt-extension' directory"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment