mirror of
https://github.com/GoogleChrome/chrome-extensions-samples.git
synced 2026-03-27 13:29:34 +07:00
Migrate chrome.printing api sample from v2 to v3 manifest (#829)
* migrate chrome.printing api sample from v2 to v3 manifest * Restore MV2 printing sample. --------- Co-authored-by: Oliver Dunk <oliverdunk@google.com>
This commit is contained in:
BIN
api-samples/printing/icons/icon.png
Normal file
BIN
api-samples/printing/icons/icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 232 B |
BIN
api-samples/printing/icons/icon128.png
Normal file
BIN
api-samples/printing/icons/icon128.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.5 KiB |
BIN
api-samples/printing/icons/icon16.png
Normal file
BIN
api-samples/printing/icons/icon16.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 227 B |
BIN
api-samples/printing/icons/icon48.png
Normal file
BIN
api-samples/printing/icons/icon48.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 765 B |
18
api-samples/printing/manifest.json
Normal file
18
api-samples/printing/manifest.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"name": "Print Extension",
|
||||
"version": "1.0",
|
||||
"description": "Sends print job directly to the printers installed on the Chromebook",
|
||||
"permissions": [
|
||||
"printing"
|
||||
],
|
||||
"action": {
|
||||
"default_popup": "printers.html",
|
||||
"default_icon": "icons/icon.png"
|
||||
},
|
||||
"icons": {
|
||||
"16": "icons/icon16.png",
|
||||
"48": "icons/icon48.png",
|
||||
"128": "icons/icon128.png"
|
||||
},
|
||||
"manifest_version": 3
|
||||
}
|
||||
18
api-samples/printing/printers.css
Normal file
18
api-samples/printing/printers.css
Normal file
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Copyright 2020 The Chromium Authors. All rights reserved.
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
table, th, td, tr {
|
||||
border: 1px solid black;
|
||||
}
|
||||
|
||||
table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
|
||||
div {
|
||||
overflow: auto;
|
||||
}
|
||||
35
api-samples/printing/printers.html
Normal file
35
api-samples/printing/printers.html
Normal file
@@ -0,0 +1,35 @@
|
||||
<!--
|
||||
* Copyright 2020 The Chromium Authors. All rights reserved. Use of this
|
||||
* source code is governed by a BSD-style license that can be found in the
|
||||
* LICENSE file.
|
||||
-->
|
||||
<!DOCTYPE HTML>
|
||||
<html>
|
||||
<head>
|
||||
<title>Printers</title>
|
||||
<link href="printers.css" rel="stylesheet" type="text/css">
|
||||
<script src="printers.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h2>Printers:</h2>
|
||||
<div id="printers">
|
||||
<table id="printersTable">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Id</th>
|
||||
<th>Name</th>
|
||||
<th>Description</th>
|
||||
<th>URI</th>
|
||||
<th>Source</th>
|
||||
<th>Default</th>
|
||||
<th>Recently used</th>
|
||||
<th>Capabilities</th>
|
||||
<th>Status</th>
|
||||
<th>Print</th>
|
||||
</tr>
|
||||
</thead>
|
||||
</table>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
99
api-samples/printing/printers.js
Normal file
99
api-samples/printing/printers.js
Normal file
@@ -0,0 +1,99 @@
|
||||
// Copyright 2020 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
function onPrintButtonClicked(printerId, dpi) {
|
||||
var ticket = {
|
||||
version: '1.0',
|
||||
print: {
|
||||
color: {type: 'STANDARD_MONOCHROME'},
|
||||
duplex: {type: 'NO_DUPLEX'},
|
||||
page_orientation: {type: 'LANDSCAPE'},
|
||||
copies: {copies: 1},
|
||||
dpi: {horizontal_dpi: dpi.horizontal_dpi, vertical_dpi: dpi.vertical_dpi},
|
||||
media_size: {
|
||||
width_microns: 210000,
|
||||
height_microns: 297000,
|
||||
vendor_id: 'iso_a4_210x297mm'
|
||||
},
|
||||
collate: {collate: false}
|
||||
}
|
||||
};
|
||||
|
||||
fetch('test.pdf')
|
||||
.then(response => response.arrayBuffer())
|
||||
.then(arrayBuffer => {
|
||||
const request = {
|
||||
job: {
|
||||
printerId: printerId,
|
||||
title: 'test job',
|
||||
ticket: ticket,
|
||||
contentType: 'application/pdf',
|
||||
document: new Blob(
|
||||
[new Uint8Array(arrayBuffer)], {type: 'application/pdf'})
|
||||
}
|
||||
};
|
||||
chrome.printing.submitJob(request, (response) => {
|
||||
if (response !== undefined) {
|
||||
console.log(response.status);
|
||||
}
|
||||
if (chrome.runtime.lastError !== undefined) {
|
||||
console.log(chrome.runtime.lastError.message);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function createPrintButton(onClicked) {
|
||||
const button = document.createElement('button');
|
||||
button.innerHTML = 'Print';
|
||||
button.onclick = onClicked;
|
||||
return button;
|
||||
}
|
||||
|
||||
function createPrintersTable() {
|
||||
chrome.printing.getPrinters(function(printers) {
|
||||
const tbody = document.createElement('tbody');
|
||||
|
||||
for (let i = 0; i < printers.length; ++i) {
|
||||
const printer = printers[i];
|
||||
chrome.printing.getPrinterInfo(printer.id, function(response) {
|
||||
const columnValues = [
|
||||
printer.id,
|
||||
printer.name,
|
||||
printer.description,
|
||||
printer.uri,
|
||||
printer.source,
|
||||
printer.isDefault,
|
||||
printer.recentlyUsedRank,
|
||||
JSON.stringify(response.capabilities),
|
||||
response.status,
|
||||
];
|
||||
|
||||
let tr = document.createElement('tr');
|
||||
for (const columnValue of columnValues) {
|
||||
const td = document.createElement('td');
|
||||
td.appendChild(document.createTextNode(columnValue));
|
||||
td.setAttribute('align', 'center');
|
||||
tr.appendChild(td);
|
||||
}
|
||||
|
||||
const printTd = document.createElement('td');
|
||||
printTd.appendChild(createPrintButton(function() {
|
||||
onPrintButtonClicked(
|
||||
printer.id, response.capabilities.printer.dpi.option[0]);
|
||||
}));
|
||||
tr.appendChild(printTd);
|
||||
|
||||
tbody.appendChild(tr);
|
||||
});
|
||||
}
|
||||
|
||||
const table = document.getElementById('printersTable');
|
||||
table.appendChild(tbody);
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
createPrintersTable();
|
||||
});
|
||||
50
api-samples/printing/test.pdf
Normal file
50
api-samples/printing/test.pdf
Normal file
@@ -0,0 +1,50 @@
|
||||
%PDF-1.7
|
||||
% ò¤ô
|
||||
1 0 obj <<
|
||||
/Type /Catalog
|
||||
/Pages 2 0 R
|
||||
>>
|
||||
endobj
|
||||
2 0 obj <<
|
||||
/Type /Pages
|
||||
/MediaBox [ 0 0 200 300 ]
|
||||
/Count 1
|
||||
/Kids [ 3 0 R ]
|
||||
>>
|
||||
endobj
|
||||
3 0 obj <<
|
||||
/Type /Page
|
||||
/Parent 2 0 R
|
||||
/Contents 4 0 R
|
||||
>>
|
||||
endobj
|
||||
4 0 obj <<
|
||||
>>
|
||||
stream
|
||||
q
|
||||
0 0 0 rg
|
||||
0 290 10 10 re B*
|
||||
10 150 50 30 re B*
|
||||
0 0 1 rg
|
||||
190 290 10 10 re B*
|
||||
70 232 50 30 re B*
|
||||
0 1 0 rg
|
||||
190 0 10 10 re B*
|
||||
130 150 50 30 re B*
|
||||
1 0 0 rg
|
||||
0 0 10 10 re B*
|
||||
70 67 50 30 re B*
|
||||
Q
|
||||
endstream
|
||||
endobj
|
||||
xref
|
||||
0 5
|
||||
0000000000 65535 f
|
||||
0000000015 00000 n
|
||||
0000000068 00000 n
|
||||
0000000161 00000 n
|
||||
0000000230 00000 n
|
||||
trailer<< /Root 1 0 R /Size 5 >>
|
||||
startxref
|
||||
456
|
||||
%%EOF
|
||||
Reference in New Issue
Block a user