BrowserExtBuild.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. class BrowserExtBuild
  3. {
  4. protected $originSource = "";
  5. public $buildDir = "";
  6. public $zipDir = "";
  7. public $zipName = "";
  8. private $info = [];
  9. public $manifest = array(
  10. 'name' => '',
  11. 'description' => '',
  12. 'version' => '',
  13. 'manifest_version' => 3,
  14. 'icons' => array(
  15. '64' => 'icon/64.png',
  16. '128' => 'icon/128.png',
  17. '192' => 'icon/192.png'
  18. ),
  19. 'externally_connectable' => array(
  20. 'matches' => array(
  21. '*://go.mtab.cc/*'
  22. )
  23. ),
  24. 'background' => array(
  25. 'service_worker' => 'src/background.js'
  26. ),
  27. 'permissions' => array(
  28. 'background',
  29. 'cookies',
  30. 'bookmarks',
  31. 'favicon'
  32. ),
  33. 'action' => array(
  34. 'default_icon' => 'icon/64.png',
  35. 'default_title' => ''
  36. ),
  37. 'host_permissions' => array(
  38. '*://go.mtab.cc/*'
  39. ),
  40. 'chrome_url_overrides' => array(
  41. 'newtab' => 'dist/index.html'
  42. )
  43. );
  44. function __construct($info)
  45. {
  46. $this->info = $info;
  47. $this->originSource = root_path('extend/browserExt');
  48. $this->buildDir = runtime_path('browserExt');
  49. $this->zipName = "browserExt.zip";
  50. $this->zipDir = public_path() . $this->zipName;
  51. }
  52. function runBuild()
  53. {
  54. if (is_dir($this->buildDir)) {
  55. $this->deleteDirectory($this->buildDir);
  56. }
  57. $this->copyDir($this->originSource, $this->buildDir);
  58. $this->copyDir(public_path() . "dist/", $this->buildDir . "/dist/");
  59. $this->delZip();
  60. $this->copyIcon();
  61. $this->renderManifest();
  62. $this->renderIndex();
  63. $this->renderInitJavascript();
  64. $this->createZipFromDir($this->buildDir, $this->zipDir);
  65. if (is_dir($this->buildDir)) {
  66. $this->deleteDirectory($this->buildDir);
  67. }
  68. return true;
  69. }
  70. function renderIndex()
  71. {
  72. $file = $this->buildDir . "dist/index.html";
  73. $f = file_get_contents($file);
  74. $option = [];
  75. $option['title'] = $this->info['ext_name'];
  76. $option['customHead'] = '<script src="../src/init.js"></script>';
  77. $option['description'] = $this->info['ext_description'];
  78. $option['favicon'] = "/icon/64.png";
  79. $option['keywords'] = '';
  80. $option['version'] = $this->info['ext_version'];
  81. $content = \think\facade\View::display($f, $option);
  82. file_put_contents($file, $content);
  83. }
  84. function renderInitJavascript()
  85. {
  86. $file = $this->buildDir . 'src/init.js';
  87. $f = file_get_contents($file);
  88. $host = explode(':', $this->info['ext_domain'])[0];
  89. $f = preg_replace("/extDomain/", $host, $f);
  90. $f = preg_replace('/extUrl/', $this->info['ext_protocol'] . "://" . $this->info['ext_domain'], $f);
  91. file_put_contents($file, $f);
  92. }
  93. function renderManifest()
  94. {
  95. $host = explode(":", $this->info['ext_domain'])[0];
  96. $this->manifest['version'] = $this->info['ext_version'];
  97. $this->manifest['name'] = $this->info['ext_name'];
  98. $this->manifest['description'] = $this->info['ext_description'];
  99. $this->manifest['action']['default_title'] = $this->info['ext_name'];
  100. $this->manifest['externally_connectable']['matches'] = ["*://{$host}/*"];
  101. $this->manifest['host_permissions'] = ["*://{$host}/*", '*://*.baidu.com/*'];
  102. file_put_contents(joinPath($this->buildDir, "manifest.json"), json_encode($this->manifest, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
  103. }
  104. //处理logo问题
  105. function copyIcon()
  106. {
  107. copy(joinPath(public_path(), $this->info['ext_logo_64']), joinPath($this->buildDir, "icon/64.png"));
  108. copy(joinPath(public_path(), $this->info['ext_logo_128']), joinPath($this->buildDir, "icon/128.png"));
  109. copy(joinPath(public_path(), $this->info['ext_logo_192']), joinPath($this->buildDir, "icon/192.png"));
  110. }
  111. //删除升级包
  112. function delZip()
  113. {
  114. if (file_exists($this->zipDir)) {
  115. unlink($this->zipDir);
  116. }
  117. }
  118. function createZipFromDir($source_dir, $output_file_path)
  119. {
  120. $zip = new ZipArchive();
  121. if ($zip->open($output_file_path, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
  122. // 递归地添加目录中的文件和子目录到压缩包
  123. $files = new RecursiveIteratorIterator(
  124. new RecursiveDirectoryIterator($source_dir),
  125. RecursiveIteratorIterator::LEAVES_ONLY
  126. );
  127. foreach ($files as $name => $file) {
  128. // 跳过 "." 和 ".." 目录
  129. if (!$file->isDir()) {
  130. $filePath = $file->getRealPath();
  131. $relativePath = mb_substr($filePath, mb_strlen(dirname($source_dir)) + 1);
  132. $zip->addFile($filePath, $relativePath);
  133. }
  134. }
  135. // 关闭压缩包
  136. $zip->close();
  137. return true;
  138. } else {
  139. abort(0, '无法创建压缩文件');
  140. }
  141. }
  142. // 递归复制目录及其内容
  143. function copyDir($source, $dest)
  144. {
  145. if (!is_dir($dest)) {
  146. mkdir($dest, 0777, true);
  147. }
  148. $files = scandir($source);
  149. foreach ($files as $file) {
  150. if ($file !== '.' && $file !== '..') {
  151. $src = $source . '/' . $file;
  152. $dst = $dest . '/' . $file;
  153. if (is_dir($src)) {
  154. $this->copyDir($src, $dst);
  155. } else {
  156. copy($src, $dst);
  157. }
  158. }
  159. }
  160. }
  161. //递归删除目录
  162. function deleteDirectory($dir)
  163. {
  164. if (!is_dir($dir)) {
  165. return;
  166. }
  167. $files = scandir($dir);
  168. foreach ($files as $file) {
  169. if ($file != '.' && $file != '..') {
  170. if (is_dir("$dir/$file")) {
  171. $this->deleteDirectory("$dir/$file");
  172. } else {
  173. unlink("$dir/$file");
  174. }
  175. }
  176. }
  177. rmdir($dir);
  178. }
  179. }