window.onload = function() {
//DsPdfViewer.LicenseKey = "***key***";
const options = {
workerSrc: "/diodocs/pdfviewer/demos/product-bundles/build/dspdfviewer.worker.js",
onBeforeOpenContextMenu: function (items, mousePosition, viewer) {
// コンテキストメニューを変更
var selectedText = viewer.getSelectedText();
if (selectedText) {
// 既存の項目を削除
items.splice(0, items.length);
// 独自のメニュー項目を追加
items.push({
type: 'button',
text: 'Googleで検索',
onClick: function () {
window.open('http://www.google.com/search?q=' + encodeURI(selectedText), '_blank');
}
});
items.push({
type: 'button',
text: 'Bingで検索',
onClick: function () {
window.open('https://www.bing.com/search?q=' + encodeURI(selectedText), '_blank');
}
});
}
return true;
},
restoreViewStateOnLoad: false,
language: "ja"
};
const viewer = new DsPdfViewer("#viewer", options);
viewer.addDefaultPanels();
viewer.mouseMode = 0;/* MouseMode.Select */
viewer.open("/diodocs/pdfviewer/demos/product-bundles/assets/pdf/StrongInTheRain.pdf");
}
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>コンテキストメニューのカスタマイズ</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./src/styles.css">
<script src="/diodocs/pdfviewer/demos/product-bundles/build/dspdfviewer.js"></script>
<script src="/diodocs/pdfviewer/demos/product-bundles/build/wasmSupportApi.js"></script>
<script src="/diodocs/pdfviewer/demos/resource/js/init.js"></script>
<script src="./src/app.js"></script>
</head>
<body>
<div id="viewer"></div>
</body>
</html>
#viewer {
height: 100%;
}