我尝试在我的 Chrome 扩展程序中使用 jQuery 和 jQuery-UI。每次创建新选项卡时,我都会尝试在内容脚本对话框窗口中创建,但我不断收到:
Property '$' of object [object Window] is not a function
我想我在加载脚本时做错了。这是我所拥有的:
{
"name":"Sample",
"description":"This is a sample",
"manifest_version":2,
"version":"2",
"background":{
"scripts":["background.js"]
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["jquery-1.8.3.min.js","jquery-ui.js","client.js","myscript.js"]
"run_at":"document_end",
"all_frames": true
}
],
"web_accessible_resources": [
"client.js","jquery-1.8.3.min.js","jquery-ui.js"
],
"permissions": [
"unlimitedStorage",
"http://*/",
"<all_urls>",
"tabs"
]
}
myscript.js(内容脚本):
var ss = document.createElement('script');
ss.type = "text/javascript";
ss.src = chrome.extension.getURL('jquery-1.8.3.min.js');
ss.onload = function() {
ss.parentNode.removeChild(ss);
console.log("jquery-1.8.3.min.js loaded");
};
var ss_ui = document.createElement('script');
ss_ui.type = "text/javascript";
ss_ui.src = chrome.extension.getURL('jquery-ui.js');
ss_ui.onload = function() {
ss_ui.parentNode.removeChild(ss_ui);
console.log("jquery-ui.js loaded");
};
var s = document.createElement('script');
s.type = "text/javascript";
s.src = chrome.extension.getURL('client.js');
s.onload = function() {
s.parentNode.removeChild(s);
console.log("client.js loaded");
};
try{
(document.head||document.documentElement).appendChild(ss);
(document.head||document.documentElement).appendChild(ss_ui);
(document.head||document.documentElement).appendChild(s);
}catch(e){
console.log("exception in appendChild");
}
client.js(我创建对话框的地方)
try{
console.log("Starting to create tabel");
var layerNode= document.createElement('div');
layerNode.setAttribute('id','dialog');
layerNode.setAttribute('title','Basic dialog');
var pNode= document.createElement('p');
pNode.innerHTML = "This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.";
layerNode.appendChild(pNode);
document.body.appendChild(layerNode);
jq162 = jQuery.noConflict(true);
(function($){
$(document).ready(function() {
$("#dialog" ).dialog();
});
})(jq162);
}
catch(e){
console.log("script in page exception in appendChild"+e);
//alert(e);
}
当我在控制台中看到我得到的打印顺序时:
Starting to create tabel client.js:2
script in page exception in appendChildTypeError: Cannot call method 'dialog' of null client.js:26
client.js loaded chrome-extension://mmoccjinakdjcmhjdjghhjnihbfkkgkp/myscript.js:24
jquery-1.8.3.min.js loaded chrome-extension://mmoccjinakdjcmhjdjghhjnihbfkkgkp/myscript.js:6
jquery-ui.js loaded
我在这里做错了什么?