{"id":494,"date":"2017-01-06T16:16:49","date_gmt":"2017-01-06T08:16:49","guid":{"rendered":"http:\/\/loveuhouse.cn\/wordpress\/?p=494"},"modified":"2017-01-06T16:26:08","modified_gmt":"2017-01-06T08:26:08","slug":"chrome-%e6%8f%92%e4%bb%b6%e5%8c%96-%e6%96%b9%e4%be%bf%e8%b0%83%e7%94%a8storage-sync%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/loveuhouse.cn\/wordpress\/?p=494","title":{"rendered":"chrome \u63d2\u4ef6\u5316 \u65b9\u4fbf\u8c03\u7528storage.sync\u51fd\u6570"},"content":{"rendered":"<h3>\u9700\u8981<\/h3>\n<ul>\n<li><a href=\"https:\/\/loveuhouse.cn\/wordpress\/?p=387\">ajax \u5c01\u88c5\u6210 \u6211\u559c\u6b22\u7684\u6837\u5b50<\/a><\/li>\n<li>jquery<\/li>\n<\/ul>\n<h3>\u8bf4\u660e<\/h3>\n<h3>c( key , value , callback )<\/h3>\n<h4>key:<\/h4>\n<ul>\n<li>\u8bf4\u660e-->\u50a8\u5b58\u952e\u4f4d\uff0c\u53ef\u9009<\/li>\n<li>\u7c7b\u578b-->string \/ object<\/li>\n<li>\u5982\u679c\u662fstring\uff0c\u652f\u6301\u70b9\u8bed\u6cd5   \u4f8b\u5982 u.getConfig <\/li>\n<li>\u5982\u679c\u662fobject\uff0c\u5c31\u4f1a\u5ffd\u7565value\u7684\u503c\uff0c\u76f4\u63a5\u628a\u5b83\u5199\u5165\u5230snyc\u50a8\u5b58<\/li>\n<\/ul>\n<h4>value:<\/h4>\n<ul>\n<li>\u8bf4\u660e-->\u4fee\u6539\u7684\u503c<\/li>\n<li>\u7c7b\u578b-->string \/ object \/ null<\/li>\n<li>\u4e0d\u5b9a\u4e49value\u4e3a\u67e5\u8be2\uff0c\u5b9a\u4e49\u4e86value\u5219\u4e3a\u4fee\u6539<\/li>\n<li>\u5982\u679c\u662fnull \uff0c \u5220\u9664\u8be5key\uff08\u652f\u6301\u70b9\u8bed\u6cd5\uff09<\/li>\n<\/ul>\n<h4>callback:<\/h4>\n<ul>\n<li>\u8bf4\u660e-->\u4fee\u6539\u7684\u503c\uff0c\u5fc5\u9009<\/li>\n<li>\u7c7b\u578b-->function<\/li>\n<li>\u7528\u4ee5\u8f93\u51fa\u8d44\u6599<\/li>\n<\/ul>\n<h3>\u5947\u6deb\u5de7\u6280<\/h3>\n<ul>\n<li><code>c(function(ret){console.log(ret)});\/\/\u8f93\u51fa\u5168\u90e8snyc\u50a8\u5b58\u6570\u636e<\/code><\/li>\n<li><code>c(\"u.getConfig\",null,function(ret){console.log(ret)});\/\/\u5220\u9664u\u4e0b\u7684getConfig<\/code><\/li>\n<\/ul>\n<h3>js<\/h3>\n<p>`<\/p>\n<p>\/\/c\u65b9\u6cd5\u5b9e\u73b0\u673a\u5236<br \/>\nfunction c(){<br \/>\n  var key = arguments[0];<br \/>\n  switch (true) {<br \/>\n    case (typeof(key)==\"undefined\"):<br \/>\n      key=null;<br \/>\n      break;<br \/>\n    case (typeof(key)==\"function\"):<br \/>\n      key=null;<br \/>\n      break;<br \/>\n    case (key==\"\"):<br \/>\n      key=null;<br \/>\n      break;<br \/>\n  }<br \/>\n  var callback,val;<br \/>\n  \/\/\u83b7\u53d6 \u56de\u8c03\u51fd\u6570 \u548c \u503c<br \/>\n  for(var i = 0 ,len=arguments.length; i &lt; len ; i++){<br \/>\n    if(typeof(arguments[i])==\"function\"){<br \/>\n      callback=arguments[i];<br \/>\n    }else{<br \/>\n      if(i!=0){<br \/>\n        val=arguments[i];<br \/>\n      }<br \/>\n    }<br \/>\n  }<br \/>\n  \/\/\u5982\u679ckey\u4e3ajson\u6570\u636e\uff0c\u76f4\u63a5\u5199\u5165\u5e76\u65ad\u5f00<br \/>\n  if(typeof(key)==\"object\"&amp;&amp;key!=null){<br \/>\n    \/\/\u76f4\u63a5\u8f93\u5165json\u6570\u636e,set<br \/>\n    chrome.storage.sync.set(key, function(result){<br \/>\n      chrome.storage.sync.get(null,function(newresult){<br \/>\n        if(typeof(callback)!=\"undefined\"){<br \/>\n          callback(newresult);<br \/>\n        }else{<br \/>\n          return newresult;<br \/>\n        }<br \/>\n      })<br \/>\n    })<br \/>\n    return;<br \/>\n  }<br \/>\n  var keyObj;<br \/>\n  var keyArr=[];<br \/>\n  \/\/\u70b9\u8bed\u6cd5\u5904\u7406<br \/>\n  keyArr=(typeof(key)!=\"string\")?[null]:key.split(\".\");<br \/>\n  chrome.storage.sync.get(keyArr[0],function(result){<br \/>\n    switch (true) {<br \/>\n      case (typeof(val)!=\"undefined\"):<br \/>\n        \/\/\u5b58\u5728val,\u5224\u65ad\u662f\u5426\u8981\u6e05\u9664<br \/>\n        if(val==null){<br \/>\n          \/\/\u6e05\u9664\u64cd\u4f5c<br \/>\n          if(keyArr.length>1){<br \/>\n            \/\/\u5220\u9664\u5bf9\u8c61\u4e2d\u7684\u4e00\u4e2a\u5c5e\u6027<br \/>\n            var data=childMerge(keyArr,undefined,result);<br \/>\n            chrome.storage.sync.set(data, function(){<br \/>\n                chrome.storage.sync.get(null,function(newresult){<br \/>\n                  if(typeof(callback)!=\"undefined\"){<br \/>\n                    callback(newresult);<br \/>\n                  }else{<br \/>\n                    return newresult;<br \/>\n                  }<br \/>\n                })<br \/>\n             })<br \/>\n          }else{<br \/>\n            \/\/\u5220\u9664\u6574\u4e2a\u5bf9\u8c61<br \/>\n            chrome.storage.sync.remove(keyArr[0], function(){<br \/>\n              chrome.storage.sync.get(null,function(newresult){<br \/>\n                if(typeof(callback)!=\"undefined\"){<br \/>\n                  callback(newresult);<br \/>\n                }else{<br \/>\n                  return newresult;<br \/>\n                }<br \/>\n              })<br \/>\n            });<br \/>\n          }<br \/>\n          return;<br \/>\n        }<br \/>\n        var data=childMerge(keyArr,val,result);<br \/>\n        chrome.storage.sync.set(data, function(){<br \/>\n            chrome.storage.sync.get(null,function(newresult){<br \/>\n              if(typeof(callback)!=\"undefined\"){<br \/>\n                callback(newresult);<br \/>\n              }else{<br \/>\n                return newresult;<br \/>\n              }<br \/>\n            })<br \/>\n         })<br \/>\n        break;<br \/>\n      default:<br \/>\n        \/\/ keyArr.splice(0,1);<br \/>\n        ret=($.isEmptyObject(result))?\"\":childFind(result,keyArr);<br \/>\n        if(typeof(callback)!=\"undefined\"){<br \/>\n          callback(ret)<br \/>\n        }else{<br \/>\n          return ret;<br \/>\n        }<br \/>\n    }<br \/>\n  })<br \/>\n}<br \/>\n\/\/\u904d\u5386\u5bfb\u627e\u5b50\u5143\u7d20<br \/>\nfunction childFind(obj,arr){<br \/>\n  if(arr[0]==null){<br \/>\n    return obj;<br \/>\n  }<br \/>\n  if(typeof(obj[arr[0]])==\"undefined\"){<br \/>\n    return undefined;<br \/>\n  }<br \/>\n  if(arr.length==1){<br \/>\n    return obj[arr[0]];<br \/>\n  }<br \/>\n  if(arr.length>1){<br \/>\n    var linshi=arr.concat();<br \/>\n    linshi.splice(0,1);<br \/>\n    return childFind(obj[arr[0]],linshi)<br \/>\n  }else{<br \/>\n    return obj[arr[0]];<br \/>\n  }<br \/>\n}<br \/>\n\/\/\u6839\u636e\u6570\u7ec4\u751f\u6210\u5bf9\u8c61<br \/>\nfunction childMerge(arr,val,orobj){<br \/>\n  var obj=clone(orobj);<br \/>\n  var linshiObj=obj;<br \/>\n  \/\/ arr=arr.reserve();<br \/>\n  for(var i = 0 , len = arr.length ; i &lt; len ; i++){<br \/>\n    var nkey=arr[i];<br \/>\n    switch (true) {<br \/>\n      case (i==len-1):<br \/>\n          linshiObj[nkey]=val;<br \/>\n        break;<br \/>\n      default:<br \/>\n        if(typeof(linshiObj[nkey])!=\"object\"&amp;&amp;i!=len-1){<br \/>\n          linshiObj[nkey]={};<br \/>\n          linshiObj=linshiObj[nkey];<br \/>\n        }else{<br \/>\n          linshiObj=linshiObj[nkey];<br \/>\n        }<br \/>\n    }<br \/>\n  }<br \/>\n  return obj;<br \/>\n}<br \/>\nfunction clone(myObj){<br \/>\n    if(typeof(myObj) != 'object' || myObj == null) return myObj;<br \/>\n    var newObj = new Object();<br \/>\n    for(var i in myObj){<br \/>\n      newObj[i] = clone(myObj[i]);<br \/>\n    }<br \/>\n    return newObj;<br \/>\n}<br \/>\n`<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u8981 ajax \u5c01\u88c5\u6210 \u6211\u559c\u6b22\u7684\u6837\u5b50 jquery \u8bf4\u660e c( key , value , callback  &hellip; <a href=\"https:\/\/loveuhouse.cn\/wordpress\/?p=494\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">chrome \u63d2\u4ef6\u5316 \u65b9\u4fbf\u8c03\u7528storage.sync\u51fd\u6570<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[24],"tags":[],"_links":{"self":[{"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/494"}],"collection":[{"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=494"}],"version-history":[{"count":6,"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/494\/revisions"}],"predecessor-version":[{"id":500,"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/494\/revisions\/500"}],"wp:attachment":[{"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}