var MarkUpChecker = (function iffe() {
'use strict';
var URLS = {
'foo': 'http://foo.com',
'bar': 'http://bar.com',
'baz': 'http://baz.com',
'yay': 'http://www.yay.com',
'blargh': 'http://www.blargh.com'
},
publicAPI;
function getURL() {
for (var i = 0; i < arguments.length; i++) {
return URLS[arguments[i]];
}
}
publicAPI = {
addURL: function() {
for (var i = 0; i < arguments.length; i += 2) {
URLS[arguments[i]] = arguments[i + 1];
}
console.dir(URLS);
return URLS;
},
addTag: function() {
var doc = document,
internal_h1 = doc.querySelectorAll('.internal_h1'),
sheet = doc.createElement('style');
for (var i = 0; i < internal_h1.length; i++) {
internal_h1[i].innerHTML = '<h1>' + internal_h1[i].innerHTML + '</h1>';
sheet.innerHTML = 'h1 {font-family: Helvetica, Arial, sans-serif !important; font-weight: 200!important; font-size: 22px !important; color: #333; margin: 3px 0px 6px; line-height: 24px !important;};'
doc.body.appendChild(sheet);
}
},
searchDoc: function() {
function insertAfter(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
var link, url, parser, newPathName = '',
emailUrl = /img\//gi,
tsUrl = /\/REPSuite\/static\/html\/inews_archives\/img\//gi,
newstr = '',
doc = document,
container,
container_id,
container_links,
container_images,
documentTableWrapper,
docBodyFirstChild,
nodeToTargetToInsertLP;
if (!doc.getElementById('container')) {
container = doc.createElement('div');
container.setAttribute('id', 'container');
container.className = 'container-avon-representative-news';
container_links = container.getElementsByTagName('a');
container_id = doc.getElementById('container');
docBodyFirstChild = doc.body.firstChild;
nodeToTargetToInsertLP = doc.getElementsByClassName('flexTile')[4];
if (nodeToTargetToInsertLP) {
documentTableWrapper = doc.getElementsByClassName('marginfix')[0];
container.appendChild(documentTableWrapper);
insertAfter(container, nodeToTargetToInsertLP);
} else {
documentTableWrapper = doc.getElementsByTagName('table')[0];
container.appendChild(documentTableWrapper);
doc.body.insertBefore(container, docBodyFirstChild);
}
} else {
container_links = doc.getElementById('container').getElementsByTagName('a');
}
container_images = container.getElementsByTagName('img');
for (var i = 0; i < container_images.length; i++) {
if (arguments[0] == "foo" || arguments[1] == "bar") {
container_images[i].src = container_images[i].src.replace(emailUrl, '/images/news/');
} else {
container_images[i].src = container_images[i].src.replace(emailUrl, '/static/images/alt_news/');
}
}
for (var i = 0, len = arguments.length; i < len; i++) {
url = getURL(arguments[i]);
for (var j = 0, jlen = container_links.length; j < jlen; j++) {
link = container_links[j];
if (link.href.indexOf(url) != -1) {
parser = document.createElement('a');
parser.href = link.href;
link.setAttribute('target', '_self');
newPathName = parser.pathname;
if (newPathName.search(/Executive|District|Division|National/) != -1) {
newPathName = newPathName.split('/').pop();
newstr = newPathName;
link.href = newstr;
}
} else {
link.setAttribute('target', '_blank');
}
}
}
}
};
return publicAPI;
})();
我的问题是我的for循环似乎在链接上运行两次并覆盖了此属性的初始添加:
link.setAttribute('target', '_self');
使用此属性:
link.setAttribute('target', '_blank');
这是一个功能的摘录,可以在上面的代码段中找到。
for (var i = 0, len = arguments.length; i < len; i++) {
url = getURL(arguments[i]);
for (var j = 0, jlen = container_links.length; j < jlen; j++) {
link = container_links[j];
if (link.href.indexOf(url) != -1) { //problem seems to be here
parser = document.createElement('a');
parser.href = link.href;
link.setAttribute('target', '_self');
newPathName = parser.pathname;
if (newPathName.search(/Executive|District|Division|National/) != -1) {
newPathName = newPathName.split('/').pop();
newstr = newPathName;
link.href = newstr;
}
} else {
link.setAttribute('target', '_blank');
}
}
}
为了给你一些背景知识,脚本的主要功能是自动执行冗余任务,我每周必须做一个新闻通讯,该新闻稿会重新用于静态网页/登录页面:
HTML:
<a href="http://foo.com/path/to_page.aspx">
<img src="img/some_image.jpg">
</a>
脚本遍历DOM,查找符合条件的URL并删除基本URL,一些路径名并将目标_self
应用于href
它也会改变我的图像的路径名!
在HTML中我调用这个接受字符串的函数:
MarkUpChecker.searchURL('foo', 'bar');
该字符串表示我想要定位的URL并应用更改:
var URLS = {
'foo': 'http://foo.com',
'bar': 'http://bar.com',
'baz': 'http://baz.com',
'yay': 'http://www.yay.com',
'blargh': 'http://www.blargh.com'
},
此功能用于循环对象:
function getURL() {
for (var i = 0; i < arguments.length; i++) {
return URLS[arguments[i]];
}
}
最后这是searchURL
函数:
searchURL: function() {
function insertAfter(newNode, referenceNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
var link, url, parser, newPathName = '',
emailUrl = /img\//gi,
tsUrl = /\/REPSuite\/static\/html\/inews_archives\/img\//gi,
newstr = '',
doc = document,
container,
container_id,
container_links,
container_images,
documentTableWrapper,
docBodyFirstChild,
nodeToTargetToInsertLP;
if (!doc.getElementById('container')) {
container = doc.createElement('div');
container.setAttribute('id', 'container');
container.className = 'container-avon-representative-news';
container_links = container.getElementsByTagName('a');
container_id = doc.getElementById('container');
docBodyFirstChild = doc.body.firstChild;
nodeToTargetToInsertLP = doc.getElementsByClassName('flexTile')[4];
if (nodeToTargetToInsertLP) {
documentTableWrapper = doc.getElementsByClassName('marginfix')[0];
container.appendChild(documentTableWrapper);
insertAfter(container, nodeToTargetToInsertLP);
} else {
documentTableWrapper = doc.getElementsByTagName('table')[0];
container.appendChild(documentTableWrapper);
doc.body.insertBefore(container, docBodyFirstChild);
}
} else {
container_links = doc.getElementById('container').getElementsByTagName('a');
}
container_images = container.getElementsByTagName('img');
for (var i = 0; i < container_images.length; i++) {
if (arguments[0] == "foo" || arguments[1] == "bar") {
container_images[i].src = container_images[i].src.replace(emailUrl, '/images/news/');
} else {
container_images[i].src = container_images[i].src.replace(emailUrl, '/static/images/alt_news/');
}
}
for (var i = 0, len = arguments.length; i < len; i++) {
url = getURL(arguments[i]);
for (var j = 0, jlen = container_links.length; j < jlen; j++) {
link = container_links[j];
if (link.href.indexOf(url) != -1) {
parser = document.createElement('a');
parser.href = link.href;
link.setAttribute('target', '_self');
newPathName = parser.pathname;
if (newPathName.search(/Executive|District|Division|National/) != -1) {
newPathName = newPathName.split('/').pop();
newstr = newPathName;
link.href = newstr;
}
} else {
link.setAttribute('target', '_blank');
}
}
}
}
我通过调试器观察到的是,当 删除它应用目标_blank
的路径名时,hrefs
另外 - 有趣的是脚本在另一个上下文/环境中工作,不需要经过这部分代码:
if (newPathName.search(/Executive|District|Division|National/) != -1) {
newPathName = newPathName.split('/').pop();
newstr = newPathName;
link.href = newstr;
}
在这种情况下它完美无缺。