Chrome扩展程序中的document.write [英] document.write in Chrome Extension

查看:158
本文介绍了Chrome扩展程序中的document.write的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此并不陌生,所以请多多包涵。我正在尝试编写执行以下操作的chrome扩展程序:

I'm new to this so please bear with me. I am trying to write a chrome extension that does the following:


  1. 检测www.website.com/anypage.html。如果检测到此网站,请执行以下操作。

  2. 不加载URL。

  3. 相反,写一个带有超链接的空白文档,网址为www.website.com/anypage.html?ie=UTF8

该脚本设置为在文档开始时运行(在清单中)。

The script is set to run at document start (in the manifest).

这是我的代码:

检测网址:

var regExp = /website.com/gi;
var match = 0;
testString = window.location.href.toString();
if(regExp.test(testString) { 
match = 1;

用UTF8编码标签写空白文档,并链接到URL:

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?

谢谢!

编辑:这是完整的代码:

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();
}


推荐答案

Chrome内容脚本具有访问权限DOM,因此您可以使用dom操作方法或innerHTML用具有锚标记的新节点替换当前页面的body元素的内容:

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:

document.body.innerHTML = "<a href=" + window.location.href + "?ie=UTF8>Title of Link</a>";

请注意,这是假定为您的Chrome扩展程序正确添加了执行DOM操作的JavaScript作为内容脚本:

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

这是我用来使其在本地运行的代码:

Here is the code I used to make it work for me locally:

manifest.json

manifest.json

{
  "name": "Test",
  "version": "1.0",
  "description": "Test",
  "permissions": [
    "<all_urls>"
  ],
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["content-script.js"],
      "run_at": "document_end"
    }
  ]
}

content-script.js

content-script.js

document.body.innerHTML = "<a href='test'>test</a>";

这篇关于Chrome扩展程序中的document.write的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆