新しいウィンドウを開いて、クリップボードにコピーしたり、印刷したり、ハード ドライブに保存したりできるデータを入力しようとしています。私はGreasemonkey user scriptを使ってこれをやっています。
スクリプトの簡略版:
window.addEventListener("load", function() {
/*EXPORT DATA*/
//Create a button
var button = document.createElement("input");
//call the actual export function
button.onclick = function() {window.exportujRozvrh();};
//I also tried this:
//button.onclick = (function() {window.exportujRozvrh();}).bind(window);
button.value = "Print data in new tab";
button.type = "button";
//Append button to a node
getElementByXpath(tisk_path).appendChild(button);
});
window.exportujRozvrh = function() {
//create a new tab (or window if tab not supported)
var okno = window.open(); //No url should be fine for same origin policy, shouldn't it?
//check if popup exists
if(okno==null)
alert("Popup blocked.");
//Write the data
okno.document.open();
okno.document.write("data");
okno.document.close();
}
この関数から実行すると、button
次のエラーがスローされます。
SecurityError: The operation is insecure.
ただし、関数がデバッグコンソールから呼び出された場合または URL バーから (入力して1)スクリプトは正常に動作します。javascript: window.exportujRozvrh();void(0)
)
したがって、この機能が問題で起動されるのはコンテキストだと思います。それが私が使用しようとしていた理由.bind(window)
です。
1)これを呼び出すと、「ウィンドウが定義されていません」というエラーがスローされます。ウィンドウが常に定義されているという事実にかなり慣れているので、これをあきらめました。