在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:bluzi/chrome-extension-execute-on-website开源软件地址:https://github.com/bluzi/chrome-extension-execute-on-website开源编程语言:JavaScript 100.0%开源软件介绍:chrome-extension-execute-on-websiteIt is a long name, but it will self-explanatory so, its worth Or is it?... What is it?It turns out that Chrome extensions does not have access to the JavaScript on a webpage, even if the extension is a content script. As a result, you can't access variables and content on the page itself. It's disapointing. Anyway, there is a solution, you have to inject a script tag to the page and then execute whatever you want in this script. You may ask yourself what is the problem? It's ugly as f, that's the problem. Well, luckily, I've got the solution! This tiny library allows you to easily execute JavaScript code in a webpage from your Chrome extension. UsageBut how does it work? It is as simple as this: exec(() => {
console.log('This is the window of the current webpage:', window);
}); Nice, huh? I know! Anyway, so that's more or less it. Best 500 bytes (unminified) your Chrome extension is going to get. InstallationjsDelivr (Recommended)I just love jsDelivr.
"permissions": [
"contentSettings"
] Under No worries, here's an example: Assuming your script is in a folder called "content_scripts": [
{
"js": [
"js/execute-on-website.min.js",
"./inject.js"
]
}
] It's as simple as that. npmNavigate to your extension's folder, and run the following command: (After making sure you have Node installed) npm i chrome-extension-execute-on-website Good, now go to your "permissions": [
"contentSettings"
] Under
And that should do the job. Here is an example: "content_scripts": [
{
"js": [
"node_modules/chrome-extension-execute-on-website/execute-on-website.js",
"./inject.js"
]
}
] Sample ManifestHere is an example of a full manifest, with the library included: {
"name": "My Extension",
"version": "0.0.4",
"manifest_version": 2,
"description": "",
"homepage_url": "http://eliran.net",
"icons": {
"16": "icons/Lightning16.png",
"19": "icons/Lightning19.png",
"48": "icons/Lightning48.png",
"128": "icons/Lightning128.png"
},
"permissions": [
"contentSettings"
],
"content_scripts": [
{
"matches": [
"*://*/*"
],
"js": [
"js/execute-on-website.min.js",
"./inject.js"
]
}
]
} |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论