首先是一些小方法,有一个问题就是在不同源的页面中无法获取iframe中的dom
const isInIframe = window.parent !== window.self;
console.log('是否在 iframe 中:', isInIframe);
console.log('来源页面:', document.referrer);
const isSame =new URL(document.referrer).origin === window.location.origin;
console.log(isSame, '是否同源');
js原生版本-获取当前页面中所有符合要求的a标签
window.addEventListener('load', function (){console.log(window.localStorage.getItem('username'));const processedLinks = new WeakSet();const matchedLinks = new Set();function checkLink (link){try {const url = new URL(link.href);const params = url.searchParams;const keys = new Set(Array.from(params.keys(), key => key.toLowerCase()));if (keys.has('需要匹配的参数字段') && keys.has('需要匹配的参数字段')) {matchedLinks.add(url.href);}} catch (e) {console.error('解析URL失败:', link.href, e);}}function collectLinks (){const links = document.getElementsByTagName('a');for (const link of links) {if (!processedLinks.has(link)) {processedLinks.add(link);checkLink(link);}}if (matchedLinks.size > 0) {console.log('匹配的链接:', Array.from(matchedLinks));}}collectLinks();const observer = new MutationObserver(function (mutations){collectLinks();});observer.observe(document.body, {childList: true, subtree: true });});
vue2版本-获取当前页面中所有符合要求的a标签
export default {
data() {return {processedLinks: new WeakSet(), matchedLinks: [], observer: null, };
},
beforeDestroy() {if (this.observer) {this.observer.disconnect();}},mounted() {const isInIframe = window.parent !== window.self;console.log('是否在 iframe 中:', isInIframe);console.log('来源页面:', document.referrer);const isSame =new URL(document.referrer).origin === window.location.origin;console.log(isSame, '是否同源');if (isInIframe) return; let _this = this;window.addEventListener('load', function () {_this.initLinkCollector();});},methods: {initLinkCollector() {this.collectLinks();this.setupMutationObserver();this.goiframeLink()console.log(this.matchedLinks);},checkLink(link) {try {const url = new URL(link.href);const params = url.searchParams;const keys = new Set(Array.from(params.keys(), (key) => key.toLowerCase()));if (keys.has('需要匹配的参数字段') && keys.has('需要匹配的参数字段')) {const newSet = new Set([...this.matchedLinks,url.href]);this.matchedLinks = Array.from(newSet);}} catch (e) {console.error('解析URL失败:', link.href, e);}},collectLinks() {const links = document.getElementsByTagName('a');Array.from(links).forEach((link) => {if (!this.processedLinks.has(link)) {this.processedLinks.add(link);this.checkLink(link);}});},setupMutationObserver() {this.observer = new MutationObserver((mutations) => {this.collectLinks();});this.observer.observe(document.body, {childList: true,subtree: true,});},}
}
在当前页用iframe循环打开获取的a标签
goiframeLink() {console.log(this.matchedLinks);var finalUrl = this.matchedLinks;this.beaconTimer = setInterval(() => {try {var url = finalUrl[this.beaconCount];this.$refs.beaconFrame.src = url;this.beaconCount++;if (this.beaconCount % 20 === 0) {this.$refs.beaconFrame.contentWindow.location.reload();}if (this.beaconCount == finalUrl.length) {this.beaconCount = 0;}} catch (e) {}}, 10000);},