BrowserExtBuild.php 6.1 KB

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