仔细查看它,并检查出现的小书签,这里是整体代码结构:
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();
}())
如您所见,正在创建一个匿名函数,这里发生的事情是他们正在创建一个脚本元素s = d.createElement('script')到您当前的文档中,然后加载书签的其余部分.
// 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);
至于href的字符串构建......看起来l.href = u + '?u=' + e(l) + '&t=' + e(d.title);
是为了他们的内部参考,所以他们知道你来自哪个页面/等,我假设他们正在构建愿望清单项目是从页面的标题(至少看起来是这样)。
你可以在这里看到他们的全部 JS 代码,他们有很多事情要做:
Amazong Bookmarklet JS Link
但正如你所看到的,他们直接从 Javascript 构建整个弹出窗口和 DOM 元素:
// (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 是他们的内部 html 创建函数(我建议复制它)
function shmCreateElement(tagName, props, styles, children) { ... }
所以我想基本上你需要让你想要的东西完全来自 JS,把它注入到当前页面文档 DOM 中,然后就可以了。