所以我这里有个小问题。我尝试为我尝试开发的附加组件运行“jpm test”。但是每次我尝试运行它时,它都会显示
console.log: bf4c_autoloader: [JavaScript Warning: "TypeError: useless expression" {file: "resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/commonjs/sdk/util/sequence.js" line: 333}]
而且我不知道在哪里搜索这个错误。我发现的唯一东西就是这个。所以这里是我使用的所有文件:
index.js(主要)
var tabs = require("sdk/tabs");
var data = require("sdk/self").data;
var prefs = require("sdk/simple-prefs").prefs;
tabs.on("ready", runScript);
//tabs.open("http://battlelog.battlefield.com/bf4");
var re = /battlelog\.battlefield\.com\/bf4/;
function runScript(tab) {
if(re.test(tab.url)){
console.log("Found battlelog tab.");
var worker = tab.attach({
contentScriptFile: data.url("bf4_checker.js")
});
worker.port.on("sendingUsername",bf4Username)
worker.port.on("sendingUserid",bf4Userid)
}else{
console.log("Looking if bf4c tab");
var worker = tab.attach({
contentScriptFile: data.url("bf4c_checker.js")
});
//see https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Content_Scripts#Communicating_with_the_add-on for further reference
worker.port.on("bf4cCheckCallback",bf4cCheckCallback)
}
}
function bf4cCheckCallback(m){
//{'bf4cPage' : false,'addownAllowed':false}
console.log(((m.bf4cPage)? 'Is a bf4c page':'Another page') + ", " + ((m.addownAllowed)? 'bf4c addon allowed!':'bf4c addon not allowed!'))
}
function bf4Username(name){
prefs.setUsername = name;
}
function bf4Userid(id){
prefs.setUserid = id;
console.log(prefs)
}
bf4_checker.js
function searchUsername(){
if(elements = document.getElementsByClassName("username")){
for (var i = 0; i < elements.length; i++) {
if(elements[i].href){
return elements[i].innerHTML;
}
};
}
return false;
}
function searchUserid(name){
if(elements = document.getElementsByClassName("wfont")){
re = new RegExp('/bf4/soldier/'+name+'/stats/(\\d+)/');
for (var i = 0; i < elements.length; i++) {
if(typeof (result = re.exec(elements[i].href)) == 'object'){
return (parseInt(result[1]))
}
}
}
return false;
}
if(username = searchUsername()){
self.port.emit('sendingUsername',username);
if(id = searchUserid(username)){
self.port.emit('sendingUserid',id);
}
}
function callback(mutations) {
var re = /(highlight)/;
mutations.forEach(function(mutationRecord) {
var sepe = splittedClasses(mutationRecord.target.className)
if(mutationRecord.attributeName == "class" && sepe.indexOf('highlight') > -1 && sepe.indexOf('popover-visible') == -1 && mutationRecord.target.className != mutationRecord.oldValue){
console.log("Got new battlereport! went from\n\t"+ mutationRecord.oldValue +"\n\t"+ mutationRecord.target.className)
}
});
}
function splittedClasses(className){
var classes = className.split(" ");
for (var i = 0; i < classes.length; i++) {
if(classes[i] == ""){
classes.splice(i,1);
}
};
return classes;
}
if(target = document.getElementById('battlereports-icon')){
console.log("initialized listener !")
var observer = new MutationObserver(callback);
observer.observe(target,{ attributes : true, attributeOldValue: true, attributeFilter : ['class'] });
}
function metaKeyUp (event) {
var key = event.keyCode || event.which;
if (key == 65) {
if(target.className != "log tools-item reports highlight"){
console.log("highlighted the report button");
target.className = "log tools-item reports highlight";
}
}
}
document.body.addEventListener("keyup",metaKeyUp.bind(this))
和最后一个 bf4c_checker.js
function isBF4CPage(){
var header = document.getElementsByTagName("meta");
for (var i = 0; i < header.length; i++) {
var curr = header[i];
if(curr.name == "x-bf4c-page"){
return {'bf4cPage' : true,'addownAllowed': (curr.content == "true")? true : false};
}
}
return {'bf4cPage' : false,'addownAllowed':false};
}
self.port.emit('bf4cCheckCallback',isBF4CPage())