我是新手,所以请多多包涵。我正在尝试编写一个执行以下操作的 chrome 扩展:
- 检测 www.website.com/anypage.html。如果检测到此网站,请执行以下操作。
- 不要加载网址。
- 相反,编写一个空白文档,其中包含指向 www.website.com/anypage.html?ie=UTF8 的超链接
该脚本设置为在文档开始时运行(在清单中)。
这是我的代码:
检测网址:
var regExp = /website.com/gi;
var match = 0;
testString = window.location.href.toString();
if(regExp.test(testString) {
match = 1;
使用 UTF8 编码标签编写带有 URL 链接的空白文档:
document.write("<a href=" + window.location.href + "?ie=UTF8>Title of Link</a>");
这不能按预期工作,只是显示一个空白页。有人有想法么?
谢谢!
编辑:这是完整的代码:
checklink(); // If there is a match, then checklink will return a 1. If it's already tagged, it will return a 5.
var matchLink = null;
if (checklink() === 1) {
matchLink = window.location.href.toString();
if (checklink() != 1) {
matchLink = null;
function checklink() { //checks to see if the current URL matches website.com
var regExp = /website.com/gi,
testString = window.location.href.toString(),
match = 0,
tagged = 0;
if (regExp.test(testString)) { //if there is a match, returns 1
match = 1;
var regExp2 = /UTF8/gi;
if (regExp2.test(testString)) { //if UTF8 is found, then it returns 5
tagged = 5;
return(match + tagged);
function tagUTF() {
if (matchLink) {
var newLink = matchLink + "?ie=UTF8";
document.write("<a href=\"" + newLink + "\">Link</a>");
if (matchLink) {
tagUTF();
}