以下是我的初步尝试。首先我创建了一个测试网页:
- test.html -
<HTML>
<SCRIPT src="script.js"></SCRIPT>
</HTML>
- 脚本.js -
function testFunction() {
console("function successfully run!");
}
然后我创建了一个非常简单的扩展来查看是否可以从内容脚本运行 testFunction():
- 清单.json -
{
"name": "Function Test",
"manifest_version": 2,
"version": "1",
"description": "An extension to experiment with running the javascript functions of the website being browsed.",
"permissions": ["<all_urls>"],
"content_scripts": [
{
"all_frames": true,
"matches": ["<all_urls>"],
"js": ["cs.js"],
"run_at": "document_end"
}
]
}
-cs.js -
scriptNodes = document.getElementsByTagName("script");
script = scriptNodes[0];
console.log(script.src);
script.testFunction();
这是控制台输出:
file:///C:/.../script.js
Uncaught TypeError: Object #<HTMLScriptElement> has no method 'testFunction'
那么,是否可以使用 Chrome 扩展程序在您正在浏览的网站上运行功能?