【发布时间】:2019-03-14 21:34:13
【问题描述】:
我想做一个书签,功能与 to this one on 9xbuddy 类似,但我希望它用于 Wikipedia 上的“引用此页”功能。这就是我所拥有的:
javascript:(function(){ window.open('https://en.wikipedia.org/w/index.php?title=Special:CiteThisPage&page=/'+ document.location.href); })();
但如果你要尝试使用这个东西,它几乎可以工作。问题在于页面 ID。所以我想知道的是,是否可以让书签获取它们?
在此先感谢(也可以运行下面的 sn-p 以轻松测试它;这样您就可以确切地看到使用书签时的结果)。
$(".selectable").click(function() {
$(this).select();
});
$(document).ready(function () {
var selectcounter = 1;
$(".selectable").each(function() {
idja = "selectable" + selectcounter;
$(this).attr('id', idja);
$(this).attr('onclick', 'selectText("' + idja + '")');
selectcounter++;
});
});
function selectText(containerid) {
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(document.getElementById(containerid));
range.select();
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(document.getElementById(containerid));
window.getSelection().addRange(range);
}
}
.selectable {
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
click below text to select it, then drag into bookmarks bar to add as bookmark
<p class="selectable">javascript:(function(){ window.open('https://en.wikipedia.org/w/index.php?title=Special:CiteThisPage&page=/'+ document.location.href); })();</p>
【问题讨论】:
-
你不能使用整个
document.location.href。您需要提取路径的最后一部分。 -
@mwoelk 我不知道该怎么做,
document.location.href是我唯一真正知道的事情。
标签: javascript wikipedia wiki bookmarklet bookmarks