【发布时间】:2016-10-09 11:54:20
【问题描述】:
我正在尝试将 jquery 插入位于我的内容脚本中的 iframe。问题是 jquery ui 确实被加载但只返回一条消息“未定义 Jquery”。我怀疑它与加载 jquery 所需的时间有关。我是否需要设置一个时间间隔才能等待 jquery 被加载?或者有什么替代方法可以让我立即使用 jquery 而无需等待?我还尝试从我的后台脚本执行 jquery 脚本,但没有成功。
内容脚本
var jsjq = iframe.contentDocument.createElement("script")
jsjq.src = chrome.extension.getURL("jquery.min.js")
jsjq.type = "text/javascript"
var jsui = iframe.contentDocument.createElement("script")
jsui.src = chrome.extension.getURL("jquery-ui.min.js")
jsui.type = "text/javascript"
var css = iframe.contentDocument.createElement("link")
css.href = chrome.extension.getURL("content.css")
css.type = "text/css"
css.rel = "stylesheet"
var cssui = iframe.contentDocument.createElement("link")
cssui.href = chrome.extension.getURL("jquery-ui.min.css")
cssui.type = "text/css"
cssui.rel = "stylesheet"
iframe.contentWindow.document.head.appendChild(jsjq)
iframe.contentWindow.document.head.appendChild(jsui)
iframe.contentWindow.document.head.appendChild(css)
iframe.contentWindow.document.head.appendChild(cssui)
var script = iframe.contentWindow.document.createElement('script')
script.type = "text/javascript"
script.innerHTML = 'if (window.jQuery) {console.log("st")} else {console.log("none")}'
iframe.contentWindow.document.head.appendChild(script)
后台脚本
chrome.tabs.executeScript(sender.tab.id, {
file: "content.js"
}, function() {
chrome.tabs.executeScript(sender.tab.id, { // this part does not seem to work at all
file: "jquery.min.js"
})})
清单
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"css": ["content.css", "jquery-ui.min.css"],
"js": ["content.js", "jquery.min.js", "jquery-ui.min.js", "async.js"]
}
],
"web_accessible_resources": [
"content.css",
"jquery.min.js",
"jquery-ui.min.js",
"jquery-ui.min.css"
]
【问题讨论】:
标签: jquery google-chrome iframe google-chrome-extension