【发布时间】:2020-12-03 21:00:37
【问题描述】:
大家好,我正在创建一个 Chrome 扩展程序,但在将一些 JS 注入用户当前所在的页面选项卡时遇到问题。
我有一个 popup.html/.js 文件和一个按钮:
popup.HTML:
<!doctype html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="/css/bootstrap.min.css" />
<link rel="stylesheet" href="/css/jquery.nok.min.css" />
<link rel="stylesheet" href="/css/materialize.min.css" />
<link rel="stylesheet" href="/css/messagebox.min.css" />
<link rel="stylesheet" href="/css/options.css" />
<link rel="stylesheet" href="/css/style.css" />
<script src="/js/jQuery.js"></script>
<script src="/js/popup.js"></script>
<script src="/js/inject.js"></script>
<script src="/js/loadingoverlay.min.js"></script>
<script src="/js/jquery.nok.min.js"></script>
<script src="/js/content.js"></script>
</head>
<body style="width: 335px; height: 55px;">
<div class="btn" id="injectIT_">Submit</div>
</body>
</html>
popup.JS:
document.addEventListener('DOMContentLoaded', function (dcle) {
$("#injectIT_").click(function() {
callInject();
});
});
Manifest.json:
{
"name" : "Blah Extention",
"version" : "11.17.2020",
"manifest_version" : 2,
"author" : "David",
"description" : "Blah Extention",
"options_page" : "/html/options.html",
"offline_enabled" : true,
"options_ui" : {
"page" : "/html/options.html",
"chrome_style" : true,
"open_in_tab" : true
},
"icons" : {
"16" : "/img/16j.png",
"48" : "/img/48j.png",
"128" : "/img/128j.png"
},
"background" : {
"scripts" : [ "/js/jQuery.js", "/js/background.js" ],
"persistent" : false
},
"browser_action" : {
"default_icon" : "/img/16jD.png",
"default_popup" : "/html/popup.html",
"default_title" : "Push this to start"
},
"web_accessible_resources" : [ "/img/*", "/js/*", "/css/*", "/html/*" ],
"content_scripts" : [ {
"matches" : [ "http://*/*", "https://*/*" ],
"all_frames" : true,
"run_at" : "document_idle",
"css" : [ "/css/messagebox.min.css", "/css/jquery.nok.min.css" ],
"js" : [ "/js/jQuery.js", "/js/content.js", "/js/messagebox.min.js",
"/js/jquery.nok.min.js", "/js/loadingoverlay.min.js", "/js/popup.js" ]
} ],
"content_security_policy" : "script-src 'self' 'unsafe-eval'; object-src 'self';",
"permissions" : ["http://*/", "https://*/", "file:///*/*", "storage", "tabs", "declarativeContent", "activeTab", "debugger", "downloads", "notifications", "unlimitedStorage", "contextMenus", "cookies", "webRequestBlocking", "nativeMessaging", "identity", "clipboardWrite"
]
}
背景.JS:
...[more js code here]
chrome.runtime.onMessage.addListener(function(request,sender,sendResponse){
if(request.message == 'Test')
console.log('Got message');
chrome.tabs.executeScript(null, { file: "/js/inject.js" }, function() {
console.log("injected!");
});
});
...[more js code here]
内容.JS:
chrome.runtime.sendMessage({ "message": "Test" });
注入.JS:
function callInject() {
var t = "14774123",
a = "qtest",
l = "Rule",
...[more js code here]
};
当我点击图标并且弹出窗口显示我点击了按钮。没有任何反应,没有错误。
我希望它做的是将 JS 注入到当前页面并继续执行 inject.js 文件中的 js 函数。
谁能发现我在这里做错了什么?
【问题讨论】:
标签: javascript html css google-chrome google-chrome-extension