BrowserExtBuild.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. if (!is_dir(joinPath($this->buildDir, 'icon'))) {
  108. mkdir(joinPath($this->buildDir, 'icon'));
  109. }
  110. copy(joinPath(public_path(), $this->info['ext_logo_64']), joinPath($this->buildDir, "icon/64.png"));
  111. copy(joinPath(public_path(), $this->info['ext_logo_128']), joinPath($this->buildDir, "icon/128.png"));
  112. copy(joinPath(public_path(), $this->info['ext_logo_192']), joinPath($this->buildDir, "icon/192.png"));
  113. }
  114. //删除升级包
  115. function delZip()
  116. {
  117. if (file_exists($this->zipDir)) {
  118. unlink($this->zipDir);
  119. }
  120. }
  121. function createZipFromDir($source_dir, $output_file_path)
  122. {
  123. $zip = new ZipArchive();
  124. if ($zip->open($output_file_path, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
  125. // 递归地添加目录中的文件和子目录到压缩包
  126. $files = new RecursiveIteratorIterator(
  127. new RecursiveDirectoryIterator($source_dir),
  128. RecursiveIteratorIterator::LEAVES_ONLY
  129. );
  130. foreach ($files as $name => $file) {
  131. // 跳过 "." 和 ".." 目录
  132. if (!$file->isDir()) {
  133. $filePath = $file->getRealPath();
  134. $relativePath = mb_substr($filePath, mb_strlen(dirname($source_dir)) + 1);
  135. $zip->addFile($filePath, $relativePath);
  136. }
  137. }
  138. // 关闭压缩包
  139. $zip->close();
  140. return true;
  141. } else {
  142. abort(0, '无法创建压缩文件');
  143. }
  144. }
  145. // 递归复制目录及其内容
  146. function copyDir($source, $dest)
  147. {
  148. if (!is_dir($dest)) {
  149. mkdir($dest, 0777, true);
  150. }
  151. $files = scandir($source);
  152. foreach ($files as $file) {
  153. if ($file !== '.' && $file !== '..') {
  154. $src = $source . '/' . $file;
  155. $dst = $dest . '/' . $file;
  156. if (is_dir($src)) {
  157. $this->copyDir($src, $dst);
  158. } else {
  159. copy($src, $dst);
  160. }
  161. }
  162. }
  163. }
  164. //递归删除目录
  165. function deleteDirectory($dir)
  166. {
  167. if (!is_dir($dir)) {
  168. return;
  169. }
  170. $files = scandir($dir);
  171. foreach ($files as $file) {
  172. if ($file != '.' && $file != '..') {
  173. if (is_dir("$dir/$file")) {
  174. $this->deleteDirectory("$dir/$file");
  175. } else {
  176. unlink("$dir/$file");
  177. }
  178. }
  179. }
  180. rmdir($dir);
  181. }
  182. }