Khi nhìn vào nó hơn, và kiểm tra việc bookmarklet xuất hiện, đây là cấu trúc tổng thể mã:
javascript: (function() {
var w = window,
l = w.location,
d = w.document,
s = d.createElement('script'),
e = encodeURIComponent,
x = 'undefined',
u = 'http://www.amazon.co.uk/wishlist/add';
if (typeof s != 'object') l.href = u + '?u=' + e(l) + '&t=' + e(d.title);
function g() {
if (d.readyState && d.readyState != 'complete') {
setTimeout(g, 200);
} else {
// testing if AUWLBook is undefined (AUWL is their global object for it)
// If it is, they add the <script> tag for their JS (variable u)
if (typeof AUWLBook == x) s.setAttribute('src', u + '.js?loc=' + e(l)),
d.body.appendChild(s);
function f() {
// they keep looping through until the Object is finally created
// Then they call the showPopover function which initializes everything
// Builds all the HTML (with JS, etc)
(typeof AUWLBook == x) ? setTimeout(f, 200) : AUWLBook.showPopover();
}
f();
}
}
g();
}())
Như bạn có thể thấy một chức năng ẩn danh đã được tạo ra, và jist của những gì đang xảy ra ở đây là họ đang tạo phần tử tập lệnh s = d.createElement ('script'), vào tài liệu hiện tại của bạn, sau đó tải phần còn lại của bookmarklet.
// since their global object will be undefined at first they create it
if (typeof AUWLBook == x) s.setAttribute('src', u + '.js?loc=' + e(l)),
d.body.appendChild(s);
Đối với việc xây dựng chuỗi cho href ... nó trông giống như l.href = u + '?u=' + e(l) + '&t=' + e(d.title);
mang tính tham khảo nội bộ của họ để họ biết những gì trang/etc bạn đến từ đâu, tôi giả sử họ đang xây dựng những gì các mục danh sách Wish là từ Tiêu đề của trang (ít nhất là nó).
Bạn có thể thấy toàn bộ mã JS của họ vào đây, họ có một toàn bộ rất nhiều xảy ra: Amazong Bookmarklet JS Link
Nhưng như bạn thấy họ xây dựng toàn bộ sổ popup & các DOMelements thẳng từ javascript:
// (part of the AUWLBook object)
showPopover : function(args){
// etc etc...
// open in window if it can't create a popover
if (!this.canDisplayPopover()) {
window.location.href = 'https://www.amazon.co.uk/wishlist/add' + '?u=' + encodeURIComponent(window.location) + '&t=' + encodeURIComponent(document.title);
return;
}
// Then comes just an insane amount of lines of creating all the elements
floater = shmCreateElement('table', { width: bookmarkletWidth, border: '0', id: 'auwlPopover' }, {position: 'absolute', zIndex: '999999999', width: bookmarkletWidth, tableLayout: 'auto', lineHeight: '100%', borderCollapse: 'collapse'});
shmCreateElement là chức năng tạo html nội bộ của chúng (tôi muốn đề nghị sao chép nó)
function shmCreateElement(tagName, props, styles, children) { ... }
Vì vậy, tôi đoán về cơ bản bạn cần phải có bất cứ điều gì bạn muốn xuất hiện hoàn toàn đến từ JS, có nó tiêm vào các trang DOM tài liệu hiện tại, và có bạn đi.
bạn có yêu cầu cho một cách để hiển thị một cửa sổ trình duyệt mà không cần bất kỳ chrome cửa sổ? Tôi sợ rằng có thể vượt quá bất cứ điều gì bạn có thể làm với javascript. – Radu
@Radu có, chính xác là, câu hỏi đã sửa đổi với ví dụ –
Cảm ơn .... [hiện tại trang này nằm trong danh sách mong muốn của tôi :-P] (http://www.amazon.co.uk/registry/wishlist/2EU2F4LZYVZI7) – Neal