這兩天接入文章頁百度分享時發現本地能夠顯示分享按鈕,可是發佈到線上以後發現分享按鈕不會展現。打開瀏覽器調試工具發現,HTTPS下的百度分享資源未加載,起初覺得是HTTPS站點下面使用的是HTTP協議的連接,可是查看源代碼發現是自適應協議的, 因此問題應該是處在百度這裏。php
打開https://bdimg.share.baidu.com/static/api/js/share.js
發現瀏覽器提示NET::ERR_CERT_COMMON_NAME_INVALID
,也就是訪問的域名和證書配置的域名不匹配
,證書的域名是*.baidu.com
,訪問的域名是bdimg.share.baidu.com
,泛域名是不能夠跨級使用的。git
雖然是百度的問題,可是咱也不可能要他去改這個分享,因此只能本身來處理了。github
將百度分享的資源打包下來進行部署api
百度分享相關JS我已經分享到github了,一鍵直達倉庫瀏覽器
文件目錄以下:服務器
|--static(百度分享資源目錄) |--DirectorySync.php |--FilterScanner.php |--index.php
DirectorySync.phphexo
/** * 目錄同步器 * Class DirectorySync * @package sync * @author xialeistudio * @date 2019-07-11 */ class DirectorySync { private $accessKey; private $secretKey; private $bucket; /** * @var Auth */ private $auth; /** * DirectorySync constructor. * @param $accessKey * @param $secretKey * @param $bucket */ public function __construct($accessKey, $secretKey, $bucket) { $this->accessKey = $accessKey; $this->secretKey = $secretKey; $this->bucket = $bucket; $this->auth = new Auth($accessKey, $secretKey); } /** * 同步目錄 * ```php * [ * '目錄名稱' => '七牛keyPrefix' * ] * ``` * @param array $list * @throws \Exception * @author xialeistudio * @date 2019-07-11 */ public function sync(array $list) { $uploader = new UploadManager(); $token = $this->auth->uploadToken($this->bucket); foreach ($list as $path => $keyPrefix) { $scanner = new FilterScanner($path); foreach ($scanner as $filename) { printf("uploading %s \n", $filename); /** @var Error $error */ list($ret, $error) = $uploader->putFile($token, $keyPrefix . $filename, $filename); if (!empty($error)) { printf("uploading %s error: %s\n", $filename, $error->message()); } } } } }
FilterScanner.phpapp
/** * 文件掃描器 * Class FilterScanner * @package sync * @author xialeistudio * @date 2019-07-11 */ class FilterScanner extends FilterIterator { /** * FilterScanner constructor. * @param $path */ public function __construct($path) { parent::__construct(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path))); } /** * Check whether the current element of the iterator is acceptable * @link https://php.net/manual/en/filteriterator.accept.php * @return bool true if the current element is acceptable, otherwise false. * @since 5.1.0 */ public function accept() { return substr($this->current(), -1, 1) != '.' && substr($this->current(), -2, 2) != '..'; } }
index.phpdom
$sync = new DirectorySync(ACCESS_KEY, SECRET_KEY, BUCKET); $sync->sync([ 'static' => '' ]);
上傳以前須要修改一下百度分享js相關的域名。打開static/api/js/share.js
,搜索到以下代碼:工具
jscfg: {domain: {staticUrl: "/"}}
將staticUrl
改爲七牛的域名,本站使用的是static.ddhigh.com
,因此改完以後的代碼以下:
jscfg: {domain: {staticUrl: "//static.ddhigh.com/"}}
完事以後執行php index.php
上傳到七牛,此時就能夠隨意部署了~。
將以往使用bdimg.share.baidu.com/static/api/js/share.js
的地方換成//static.ddhigh.com/static/api/js/share.js
便可,其餘資源會自動加載。
本站的CDN域名作了防盜鏈處理,各位若是直接用個人share.js連接是會有問題的
上傳到七牛這個步驟完成以後,搜索文件baidushare.swig
,找到最下面的以下代碼:
with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='//bdimg.share.baidu.com/static/api/js/share.js?cdnversion='+~(-new Date()/36e5)];
將連接替換爲本身的CDN連接,我這邊替換後的結果以下:
with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='//static.ddhigh.com/static/api/js/share.js?cdnversion='+~(-new Date()/36e5)];
更多精彩內容盡在個人博客天天進步一點點