Google 크롬 확장 프로그램을 개발하는 데 문제가 있습니다. 내 특정 코드를 실행하기 위해 특정 npm 모듈이 필요하므로 Browserify를 살펴 보았습니다. 문제없이 모든 단계를 따라 갔지만 코드를 실행하면 오류가 발생합니다. 스크린 샷은 아래에 첨부되어 있습니다.Google 크롬 확장 프로그램에서 Browserify 및 NodeJS 통합 관련 문제
Error when Chrome extension is only loaded
내 모든 파일은 동일한 프로젝트 폴더 (popup.html, popup.js, bundle.js 등)에 있습니다. 나는 하나의 html 파일과 하나의 자바 스크립트 파일 (bundle.js 제외) 만 가지고있다. 여기 내 popup.html 코드 :
document.addEventListener('DOMContentLoaded', function() {
\t var convertMP3Button = document.getElementById("getLinkAndConvert");
\t convertMP3Button.addEventListener("click", function() {
\t \t chrome.tabs.getSelected(null, function(tab) { // 'tab' has all the info
\t \t \t var fs = require('fs');
\t \t \t var ytdl = require('ytdl-core');
\t \t \t var ffmpeg = require('fluent-ffmpeg');
\t \t \t var ffmetadata = require("ffmetadata");
\t \t \t var request = require('request');
\t \t \t console.log(tab.url); //returns the url
\t \t \t convertMP3Button.textContent = tab.url;
\t \t \t var url = tab.url;
\t \t var stream = ytdl(url);
\t \t //.pipe(fs.createWriteStream('/Users/nishanth/Downloads/video.mp4'));
\t \t // Helper method for downloading
\t \t var download = function(uri, filename, callback){
\t \t request.head(uri, function(err, res, body){
\t \t request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
\t \t });
\t \t };
\t \t \t ytdl.getInfo(url, function(err, info) {
\t \t console.log("INFO: " + JSON.stringify(info, null, 2));
\t \t var process = new ffmpeg({source:stream})
\t \t process.save('/Users/nishanth/Downloads/' + info.title + '.mp3').on('end', function() {
\t \t console.log("PROCESSING FINISHED!");
\t \t download(info.thumbnail_url, "/Users/nishanth/Downloads/image.jpg", function() {
\t \t console.log("DOWNLOADED IMAGE");
\t \t var options = {
\t \t artist: info.author,
\t \t attachments: ["/Users/nishanth/Downloads/image.jpg"]
\t \t };
\t \t ffmetadata.write('/Users/nishanth/Downloads/' + info.title + '.mp3', {}, options, function(err) {
\t \t if (err)
\t \t console.error("Error writing cover art: " + err);
\t \t else
\t \t console.log("Cover art added");
\t \t });
\t \t });
\t \t });
\t \t });
\t \t });
\t });
});
<!doctype html>
<html>
<head>
<title>Youtube Music</title>
<script src="bundle.js"></script>
<script src="popup.js"></script>
</head>
<body>
<h1>Youtube Music</h1>
<button id="getLinkAndConvert">Download Song Now!</button>
</body>
</html>
내가 제대로 NPM 모듈을 사용하기 위해 browserify 통합 할 수 없었던 이유를 찾을 수 있다면 그것은 좋은 것입니다 .
나는 browserify-fs로 수정 프로그램을 구현했지만, 문제는 내 ffmpeg 라이브러리에 있다고 생각합니다. 분명히 브라우저에 자식 프로세스에 문제가있어 ffmpeg 라이브러리의 생성 기능이 작동하지 않습니다. 나는 그것을 여기에서 발견했다 : https://github.com/babel/babelify/issues/175. –
예, 브라우저에서 자식 프로세스를 생성하는 것은 실제로 불가능합니다. 가장 가까운 것은 WebWorker입니다. browserify는 실제로 브라우저에서 node.js 코드를 실행할 수는 없으며, shim을 내장 한 commonjs 모듈의 사전 패키지 도구 일 뿐이라는 것을 기억하십시오. – Grinde