javascript - Browserify with chrome extension:无法读取未定义的属性“_handle”
问题描述
我想在 chrome 扩展中使用 npm 模块,所以我使用 browserify。
manifest.json
:
{
"name": "foo",
"version": "1.0",
"description": "foo",
"permissions": ["tabs", "<all_urls>"],
"browser_action": {
"default_popup": "popup.html",
"default_icon": {
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"48": "images/get_started48.png",
"128": "images/get_started128.png"
}
},
"icons": {
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"48": "images/get_started48.png",
"128": "images/get_started128.png"
},
"manifest_version": 2
}
popup.html
:
<!DOCTYPE html>
<html>
<head>
<script src='bundle.js'></script>
</head>
<body style="width:400px;">
<div id='message'>Fetching HTML content...</div>
</body>
</html>
popup.js
:
chrome.runtime.onMessage.addListener(function(request, sender) {
if (request.action == "getSource") {
// Division-content = message.innerText
// HTML-fetched = request.source
// HTML content
// Indices of nick-names are in indices[]
var htmlContent = request.source;
var regex = /nick-name="/gi, result, indices = [];
while(( result = regex.exec(htmlContent) )) {
indices.push(result.index);
}
// User ID's
// Nick-names are in names[]
var names = [];
var i;
for (i = 0; i < indices.length; i++) {
var thisIndex = indices[i]+11; // index of the 1st char in ID
var thisName = '';
// Substring: [i+11 → double-quote)
while (htmlContent[thisIndex] !== '"') {
thisName = thisName.concat(htmlContent[thisIndex]); // Add current char to name
thisIndex++; // move to next char
}
names.push(thisName);
}
// Debug: Check indices
/*
var contentI = '';
var x;
for (x = 0; x < indices.length; x++) {
contentI = contentI.concat(indices[x].toString());
contentI = contentI.concat('\n');
}
message.innerText = contentI;
*/
// Debug: Check Names
var contentNames = '';
var y;
for (y = 0; y < names.length; y++) {
contentNames = contentNames.concat(names[y]);
contentNames = contentNames.concat('\n');
}
message.innerText = contentNames;
var nodejieba = require("nodejieba");
var resultjieba = nodejieba.cut("barbarbar");
message.innerText = resultjieba;
}
});
function onWindowLoad() {
var message = document.querySelector('#message');
chrome.tabs.executeScript(null, {
file: "getPagesSource.js" // I don't think this js file is related to this question so I won't post it here
}, function() {
// If you try and inject into an extensions page or the webstore/NTP you'll get an error
if (chrome.runtime.lastError) {
message.innerText = 'There was an error injecting script : \n' + chrome.runtime.lastError.message;
}
});
}
window.onload = onWindowLoad;
我使用browserify popup.js -o bundle.js
命令(基于 Browserify 的hello-world 示例)捆绑popup.js
和所需的 npm 模块,但浏览器返回错误消息“事件处理程序中的错误:TypeError:无法读取未定义的属性'_handle'”。
bundle.js
太长。我不会在这里发布它,但问题出在这一行......
module.exports = function (blocking) {
[process.stdout, process.stderr].forEach(function (stream) {
if (stream._handle && stream.isTTY && typeof stream._handle.setBlocking === 'function') {
stream._handle.setBlocking(blocking)
}
})
解决方案
推荐阅读
- javascript - 如何使用来自 for 循环的信息来编写电子邮件
- php - 打开页面时删除url中的某些部分
- javascript - 当用户单击/移出文本框时删除错误验证消息
- ios - 它们是否相当于原生 iOS 模拟器开发的反应原生“热重载”?
- html - 寻找正确的 Laravel 路线 | 从数据库中获取照片
- c - 我的快速排序实现不起作用
- google-colaboratory - Google Colab 中的运行时和会话有什么区别?
- r - 在数据框的不同行之间的管道内计算
- python - 如何限制 Jupyter Notebook 允许使用的 RAM 量?
- macos - aws cli 自动缩放命令不返回到 shell promot