Chrome 扩展中的 document.write
我对此很陌生,所以请耐心等待。我正在尝试编写一个 chrome 扩展来执行以下操作:
- 检测 www.website.com/anypage.html。如果检测到该网站,请执行以下操作。
- 不要加载 URL。
- 相反,编写一个空白文档,其中包含指向 www.website.com/anypage.html?ie=UTF8 的超链接
。该脚本设置为在文档开始时运行(在清单中)。
这是我的代码:
检测 URL:
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();
}
I'm new to this so please bear with me. I am trying to write a chrome extension that does the following:
- Detect www.website.com/anypage.html. If this website is detected, then do the following.
- Don't load the URL.
- Instead, write a blank document with a hyperlink to www.website.com/anypage.html?ie=UTF8
The script is set to run at document start (in the manifest).
Here is my code:
Detect URL:
var regExp = /website.com/gi;
var match = 0;
testString = window.location.href.toString();
if(regExp.test(testString) {
match = 1;
Write blank document with link to the URL with the UTF8 encoding tag:
document.write("<a href=" + window.location.href + "?ie=UTF8>Title of Link</a>");
This doesn't work as expected, and just shows a blank page. Anyone have any ideas?
Thanks!
EDIT: Here is the full code:
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
chrome 内容脚本可以访问 DOM,因此您可以使用 dom 操作方法或 innerHTML 将当前页面的 body 元素的内容替换为具有锚标记的新节点:
请注意,这假设 JavaScript正在执行 DOM 操作的代码已作为“内容脚本”正确添加到您的 Chrome 扩展中:
http://code.google.com/chrome/extensions/content_scripts.html
编辑:
这是我用来使其在本地工作的代码:
manifest.json
content-script。 js
The chrome content script has access to the DOM, so you could just replace the contents of the body element of the current page with a new node that has your anchor tag either using dom manipulation methods or innerHTML:
Please note, this assumes that the JavaScript that is doing the DOM manipulation was properly added for your Chrome extension as a "content script":
http://code.google.com/chrome/extensions/content_scripts.html
EDIT:
Here is the code I used to make it work for me locally:
manifest.json
content-script.js