我嘗試使用jQuery和jQuery的UI在我的Chrome擴展程序時使用jQuery的內容腳本解析錯誤。 我嘗試在每次創建新的選項卡中的內容腳本對話框窗口創建,但我不斷收到:鉻extention創建對話框窗口
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
我在做什麼錯在這裏?
你什麼時候會包括'script.js'? –
對不起,它的client.js(更新問題) – user63898
你檢查過,看看是否jquery的'$'在myscript.js中正確定義? –